在数据库中获取有关更改/插入的通知的标准方法

I am curently trying to make a chat application aimed at 1000-1500 users. What currently happens is once my webpage loads I make an ajax request every second to check if there is anything new in the database. I want to know if this is the standard practise or if there is a more efficient way to be notified by the server somehow when an insertion occurs.

Use WebSockets. Or at the very least AJAX polling. Firing a request every second from 1500 clients will most likely kill your server.

Look at http://socket.io/ if you are open to introduce something new to your stack. But there are PHP websocket solutions out there if you are limited to PHP.

Your approach is a standard method called Polling. Based on the number of clients this should be perfectly fine for a server with up-to-date hard-ware (do HEAD requests via AJAX that indicate the status via HTTP status code).

The other alternative - as pointed out by Jan - is called Pushing.

  • Pros: Involves a lot less requests to the server.
  • Cons: Requires technology that may or may not be provided by your client's browser.

In case you'll opt for the second approach, take a look into Server-Sent Events (W3C draft).

This specification defines an API for opening an HTTP connection for receiving push notifications from a server in the form of DOM events. The API is designed such that it can be extended to work with other push notification schemes such as Push SMS.