All examples By author By category About

nitaku

Server-sent events

See this Gist for the full source.

A simple test for server-sent events, based on this Mozilla Developer Network page.

Every second, a PHP server triggers a ping event, that is passed to the client. This invokes a simple callback that writes a message on a log.

The example also makes use of standard DOM manipulation instead of jQuery and the like (this has been used as reference for prepending a node).

WARNING! In order for this example to work, the provided PHP is invoked with an absolute URL. If you want to change the server-side code, please remember to change this URL to point to your copy (this time you should use a relative address, I think).

P.S. I have no idea if and when the server script is terminated, or if it is looping endlessly. I think that the script is terminated when the user leaves the page or deletes the EventSource object (PHP has a ignore_user_abort function to avoid script termination in such cases).