Webessence
Tags

Server Sent Events

Javascript | 2012-02-18 18:52:57 | Comments (0)

Server Sent Events (SSE) can be used to get "push" notifications from the server. It is really a standardized way of polling the server for new notifications (COMET). The client uses the EventSource object for this. This object makes a request to the URL and when it receives data, it fires events.

var source = new EventSource("http://www.mysite.com/updates.php");
source.addEventListener("message", function(e) {
console.log(e.data);
}, false);

The server should not closes the connection, but keeps it open. When the connection is closed, the client will automatically reconnect. The server can send a "retry" command with the number of milliseconds that the client should wait to reconnect. Because the webserver should not closes the connection, Apache is not really suitable for this. Servers like node.js are better, because of the event loop.

Some good links:

Comments

No comments.
Comments are turned off after 90 days.