Creates a new instance of the driver.
Optionaloptions: {Optional configuration for the driver.
OptionalEventSource?: SseEventSourceConstructorOptional EventSource constructor, such as the
eventsource package in Node.js. Defaults to the global one.
Optionalpublisher?: RequestInitPublisher options for the HTTP requests (fetch).
Optionalsubscriber?: EventSourceInitSubscriber options for the EventSource instance.
Optionalurl?: stringURL to connect to via SSE. Defaults to "/.well-known/mercure".
Indicates whether the driver is currently active. Subclasses must keep it
in sync with their connection: signaling messages are only published while
the driver is active, and joined rooms announce themselves again when it
becomes active. A namespace that fails on its own leaves the driver
active, and emitting active once it is subscribed again makes rooms
announce themselves to the peers they missed.
Sets the active state of the driver and emits corresponding events.
Destroys the driver instance, cleaning up any resources.
Emits inactive if the driver was active, then removes all event handlers.
Emits an internal event with optional data.
The event name.
The data to pass to event handlers.
Unregisters an event handler for the specified internal event.
The event name.
The event handler function to remove.
Registers an event handler for the specified internal event.
The event name.
The event handler function.
Optionaloptions: { signal?: AbortSignal }
Listener options.
Optionalsignal?: AbortSignalRemoves the handler when aborted.
A function that removes the handler.
Publishes a signaling message to the specified namespace.
The namespace to publish the message to.
The message data to publish.
Nothing directly; resolves once the message is delivered (async drivers may return a promise).
Subscribes to signaling messages for the specified namespace.
The namespace to subscribe to.
The handler function to call when a message is received.
Nothing directly; resolves once the subscription is established (async drivers may return a promise).
Unsubscribes from signaling messages for the specified namespace.
The namespace to unsubscribe from.
The handler function to remove.
Nothing directly; resolves once the unsubscription is confirmed (async drivers may return a promise).
Server-Sent Events (SSE) signaling driver.
SSE is a unidirectional communication protocol that allows servers to push real-time updates to clients over a single HTTP connection. This driver uses SSE to receive updates from the server. To send messages to the server, it uses standard HTTP POST requests. This protocol can be used as an alternative to WebSocket-based transport. If a connection doesn't open within 10 seconds, subscribing fails. A connection that the browser stops retrying, such as after an HTTP error while the server restarts, is reopened with a backoff of up to 30 seconds.
Each subscribed namespace holds its own connection, and a joined room subscribes two. Over HTTP/1.1, browsers allow only 6 connections per host, shared with the publish requests, so serve the endpoint over HTTP/2 (as Mercure does with TLS) when a page joins more than two rooms.
In Node.js there is no global
EventSourceorlocation: pass an implementation such as theeventsourcepackage asoptions.EventSource, and an absoluteurl.Example
Client-side code (browser with SSE support):
You can use this JWT publisher key with the Mercure server's default configuration for testing purposes:
Running a local Mercure server for testing:
Instead of using Mercure, you can use a Node.js server: