Server-Sent Events allows a web page for getting updates from a server.
Server-Sent Events - One Way Messaging
A server-sent event is used when a web page is automatically getting updates from a server.
Eventhough it was also possible earlier, but the web page would have to verify if any updates were available. Using the server-sent events, the updates will be coming automatically.
Examples: Facebook/Twitter updates, stock price updates, news feeds, sport results, etc.
The table numbers specify the first browser version which fully support server-sent events.
Receive Server-Sent Event Notifications
The EventSource object is used for receiving server-sent event notifications:
Check Server-Sent Events Support
In the tryit example given above there are some extra lines of code for checking browser support for server-sent events:
Server-Side Code Example
For the example given above to work properly, you will be needing a server which is capable of sending data updates (like PHP or ASP).
The server-side event stream syntax is very simple. Set the "Content-Type" header to "text/event-stream". Now you can send event streams.
Code given in PHP (demo_sse.php):
Code in ASP (VB) (demo_sse.asp):
The EventSource Object
In the examples given above, the onmessage event is used to get messages. But, other events are also available:
|onopen||while opening a connection to the server|
|onmessage||While receiving a message|
|onerror||while an error occurs|