Supported by
Java script
SSE are not part of WebSockets, defines an API for opening an HTTP connection for receiving push notifications from a server.
SSEs are sent over traditional HTTP. That means they do not require a special protocol or server implementation to get working. In addition, Server-Sent Events have a variety of features that WebSockets lack by design such as automatic reconnection, event IDs, and the ability to send arbitrary events.
Events
Open: when a new SSE connection is opened.
Message: when the client receives a new message.
Error: when there any connection error like a disconnection.
JavaScript API
To subscribe to an event stream, create an EventSource object and pass it the URL of your stream:
var sse = new EventSource('sse.html');
sse.addEventListener('message', function(e)
{console.log(e.data);
}, false);
sse.addEventListener('open', function(e) {
// Connection was opened.
}, false);
sse.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED) {
// Connection was closed.
}
}, false);
When updates are pushed from the server, the onmessage handler fires and new data is available in its e.data property. If the connection is closed, the browser will automatically reconnect to the source after ~3 seconds (this is a default retry interval, you can change on the server side).
Fields
The following field names are defined by the specification:
event
The event's type. If this is specified, an event will be dispatched on the browser to the listener for the specified event name; the web site would use addEventListener() to listen for named events. the onmessage handler is called if no event name is specified for a message.
data
The data field for the message. When the EventSource receives multiple consecutive lines that begin with data:, it will concatenate them, inserting a newline character between each one. Trailing newlines are removed.
id
The event ID to set the EventSource object's last event ID value to.
retry
The reconnection time to use when attempting to send the event. This must be an integer, specifying the reconnection time in milliseconds. If a non-integer value is specified, the field is ignored.
All other field names are ignored.
For multi-line strings use #10 as line feed.
Examples of use:
If you need to send a message to a client, just use WriteData method.
// If you need to send a message to a client, just use WriteData method.
Connection.WriteData('Notification from server');
// To send a message to all Clients, use Broadcast method.
Connection.Broadcast('Notification from server');
// To send a message to all Clients, use Broadcast method.
Connection.Broadcast('Notification from server');
// To send a message to all Clients using url 'sse.html', use Broadcast method and Channel parameter:
Connection.Broadcast('Notification from server', '/sse.html');
// You can send a unique id with an stream event by including a line starting with "id:":
Connection.WriteData('id: 1' + #10 + 'data: Notification from server');
// If you need to specify an event name:
Connection.WriteData('event: notifications' + #10 + 'data: Notification from server');
javascript
code to listen "notifications" channel:
sse.addEventListener('notifications', function(e) {
console.log('notifications:' + e.data);
}, false);