Getting started
Server-sent events (SSE) is a standardised protocol that allows web-servers to push data (characterized as events) to a client without the client having to request it immediately before.
Using SSE can allow for significant savings in bandwidth and battery life on portable devices and will work with your existing infrastructure as it operates directly over the HTTP protocol without the need for the connection upgrade that WebSockets and HTTP/2 do (but can also be used with HTTP/2!).
Compared to WebSockets it has comparable performance and bandwidth usage, especially over HTTP/2, and natively includes event ID generation and automatic reconnection when clients are disconnected.
- Comparison: Server-sent Events vs WebSockets vs Polling
- WHATWG standards section for server-sent events
- MDN guide to server-sent events
How does it work?
SSE works by a client connecting to a server using the EventSource interface. The server then indicates in its response headers that it will send back data in a stream of events and keeps the connection open indefinitely until it is closed by the client. From this point the server is free to continuously write data to the open connection and the EventSource will emit events with the sent data.
- Client makes a request to the server using EventSource.
- Server responds with the
Content-Type
header set totext/event-stream
. - Client emits the
open
event and is ready to receive data. - Server continually writes text data to the open connection.
- Client receives data and emits events on the
EventSource
instance. - Client terminates connection by calling the
close
method.
The technology can be used for things for things such as live notifications, news tickers, chat rooms, shout-boxes, event logs, progress bars, etc.
Install
Better SSE is shipped as a package on npm. You can install it with any Node package manager.
TypeScript types are included in the package distribution. No need to install from DefinitelyTyped for TypeScript users!
Create a session
Sessions simply represent an open connection between a client and the server. The client will first make a request to the server, and the server will open a session that it will push data to the client with.
First import the module:
Then create a session when the client makes a request on the specified route (GET /sse
in this case):
Now that we have an active session we can use it to push an event to the client:
This will push an event named ping
(but this can be any string) with the string Hello world!
as its associated data.
Connect from the client
From your client-side code you can now connect to the server at the defined path.
First we open a connection to the server to begin receiving events from it:
Then we can attach an event listener to listen for the event our server is going to send:
If you check your browser console you will now see ping | "Hello world!"
logged to the console. Easy!
You can find a reference to the received event object interface under the MessageEvent
page on MDN.