Below is a breakdown of the SSEventStream object made available through the Response.sse property for requests being eligible for Server-Sent Events based communication.
Server-Sent Events are essentially a HTTP request that stays alive and gradually receives data from the server until disconnection. With this in mind, this functionality is provided through the Response.sse property on the Response object. You may not set the HTTP status or write any headers after a SSEventStream has been opened on a Response object.
See below for an example of a simple news events endpoint using Server-Sent Events:
const crypto = require('crypto');
const sse_streams = {};
function broadcast_message(message) {
// Send the message to each connection in our connections object
Object.keys(sse_streams).forEach((id) => {
sse_streams[id].send(message);
})
}
webserver.get('/news/events', (request, response) => {
// You may perform some authentication here as this is just a normal HTTP GET request
// Check to ensure that SSE if available for this request
if (response.sse) {
// Looks like we're all good, let's open the stream
response.sse.open();
// OR you may also send a message which will open the stream automatically
response.sse.send('Some initial message');
// Assign a unique identifier to this stream and store it in our broadcast pool
response.sse.id = crypto.randomUUID();
sse_streams[response.sse.id] = response.sse;
// Bind a 'close' event handler to cleanup this connection once it disconnects
response.once('close', () => {
// Delete the stream from our broadcast pool
delete sse_streams[response.sse.id]
});
} else {
// End the response with some kind of error message as this request did not support SSE
response.send('Server-Sent Events Not Supported!');
}
});| Property | Type | Description |
|---|---|---|
active |
Boolean |
Whether this SSE stream is still active. |
open(): Opens the Server-Sent Events stream.- Returns a
Booleanwhich signifies whether this stream was successfully opened or not. - Note this method will automatically be called on your first
send()if not already called yet.
- Returns a
close(): Closes the Server-Sent Events stream.- Returns a
Booleanwhich signifies whether this stream was successfully closed or not.
- Returns a
comment(data: string): Sends a comment type message to the client that will NOT be handled by the client EventSource.- Returns a
Booleanwhich signifies whether this comment was successfully sent or not. - Note this can be useful as a keep-alive mechanism if messages might not be sent regularly.
- Returns a
send(...3 Overloads): Sends a message to the client with the specified custom id, event and data.- Overload Types:
send(data: string): Sends a message with the specifieddata.send(event: string, data: string): Sends a message on the customeventwith the specifieddata.send(id: string, event: string, data: string): Sends a message with a customidon the customeventwith the specifieddata.
- Returns as
Booleanwhich signifies whether this message was sent or not. - Note this method will automatically call the
open()method if not already called yet. - Note messages sent with just the
dataparameter will be handled bysource.onmessage/messageevent on the client-sideEventSource. - Note messages sent with both the
event/dataparameters will be handled by the appropriateeventlistener on the client-sideEventSource.
- Overload Types: