Extends from EventEmitter.
A Session represents an open connection between the server and the client.
It emits the connected
event after it has connected and flushed all headers to the client, and the disconnected
event after client connection has been closed.
req
is an instance of IncomingMessage.
res
is an instance of ServerResponse.
options
is an object with the following properties:
Property | Type | Default | Description |
---|---|---|---|
serializer |
function |
JSON.stringify |
Serialize data to a string that can be written over the wire. Note that only values written with .data() or .push() are serialized, as everything else is assumed to already be a string. |
sanitizer |
function |
Sanitize values so as to not prematurely dispatch events when writing fields whose text inadvertently contains newlines. By default, CR, LF and CRLF characters are replaced with a single LF character ( \n ) and then any trailing LF characters are stripped so as to prevent a blank line being written and accidentally dispatching the event before .dispatch() is called. |
|
trustClientEventId |
boolean |
true |
Whether to trust the last event ID given by the client in the Last-Event-ID request header.When set to false , the lastId property will always be initialized to an empty string. |
retry |
number | null |
2000 |
Time in milliseconds for the client to wait before attempting to reconnect if the connection is closed. This is equivalent to immediately calling .retry().dispatch() after a connection is made.Give as null to avoid sending an explicit reconnection time and allow the client browser to decide itself. |
keepAlive |
number | null |
10000 |
Time in milliseconds interval for the session to send a comment to keep the connection alive. Give as null to disable the keep-alive mechanism. |
statusCode |
number |
200 |
Status code to be sent to the client. Event stream requests can be redirected using HTTP 301 and 307 status codes. Make sure to set Location header when using these status codes (301/307) using the headers property.A client can be asked to stop reconnecting by send a 204 status code. |
headers |
object |
{} |
Additional headers to be sent along with the response. |
The last ID sent to the client.
This is initialized to the last event ID given by the user (in the Last-Event-ID
header), and otherwise is equal to the last number given to the .id
method.
Indicates whether the session and connection is open or not.
Custom state for this session.
Use this object to safely store information related to the session and user.
Use module augmentation to safely add new properties to the SessionState
interface.
Flush the buffered data to the client by writing an additional newline.
Set the event to the given name (also referred to as "type" in the specification).
Write arbitrary data onto the wire.
The given value is automatically serialized to a string using the serializer
function which defaults to JSON stringification.
Set the event ID to the given string.
Passing null
will set the event ID to an empty string value.
Set the suggested reconnection time to the given milliseconds.
Write a comment (an ignored field).
This will not fire an event, but is often used to keep the connection alive.
Create and dispatch an event with the given data all at once.
This is equivalent to calling .event()
, .id()
, .data()
and .dispatch()
in that order.
If no event name is given, the event name (type) is set to "message"
.
If no event ID is given, the event ID (and thus the lastId
property) is set to a string of eight random characters (matching a-z0-9
).
Emits the push
event with the given data, event name and event ID in that order.
Pipe readable stream data to the client.
Each data emission by the stream emits a new event that is dispatched to the client.
This uses the push
method under the hood.
options. |
Type | Default | Description |
---|---|---|---|
eventName |
string |
"stream" |
Event name to use when dispatching a data event from the stream to the client. |
Iterate over an iterable and send yielded values as data to the client.
Each yield emits a new event that is dispatched to the client.
This uses the push
method under the hood.
options. |
Type | Default | Description |
---|---|---|---|
eventName |
string |
"iteration" |
Event name to use when dispatching a data event from the yielded value to the client. |
Creates and returns a promise that resolves to an instance of a Session once it has connected.
It takes the same arguments as the Session class constructor.
Extends from EventEmitter.
A Channel is used to broadcast events to many sessions at once.
Custom state for this channel.
Use this object to safely store information related to the channel.
List of the currently active sessions subscribed to this channel.
You should not mutate the contents of this array.
Number of sessions subscribed to this channel.
Equivalent to channel.activeSessions.length
.
Register a session so that it will start receiving events from this channel.
Note that a session must be connected before it can be registered to a channel.
Fires the session-registered
event with the registered session as its first argument.
Deregister a session so that it no longer receives events from this channel.
Note that sessions are automatically deregistered when they are disconnected.
Fires the session-deregistered
event with the session as its first argument.
If the session was disconnected the channel will also fire the session-disconnected
event with the disconnected session as its first argument beforehand.
Broadcasts an event with the given name and data to every active session subscribed to the channel.
Under the hood this calls the push
method on every active session.
If no event name is given, the event name (type) is set to "message"
.
Emits the broadcast
event with the given data and event name in that order.
options. |
Type | Default | Description |
---|---|---|---|
filter |
(session: Session) => boolean |
Filter sessions that should receive the event. Called with each session and should return true to allow the event to be sent and otherwise return false to prevent the session from receiving the event. |
Creates and returns an instance of a Channel.
It takes the same arguments as the Channel class.