- API documentation
- Table of contents
- Hub
Hub#constructor(server, config)->HubHub#listen(port, addr)->Promise<void>Hub#dispatchUpdate(topics, data, opts)->Promise<Number>Hub#generateJwt(claims)->Promise<String>Hub#generatePublishJwt(targets)->Promise<String>Hub#generateSubscribeJwt(targets)->Promise<String>Hub#authorizePublish(req)->Promise<Object>Hub#authorizeSubscribe(req)->Promise<Object>Hub#end(opts)->Promise<void>Hub#endSync()->voidHub#changeJwtKey()->Promise<void>Hub#killSwitch()->Promise<void>
- Server
- Publisher
Initializes a new Hub instance. When clustering the application, it is required to connect the hub to Redis in order to leverage its pub/sub capabilities and scale across multiple hub instances.
The instance does not immediately "listen". Calling the Hub#listen() method is required.
Arguments :
server(http.Server, optional) : a nativehttp.Serverinstance. If not passed, the hub will create one.config(Object, optional) :id(String, optional) : a unique ID identifying this instance amongst a full instances cluster.jwtKey(String, required) : the publisher's AND subscriber's JSON Web Token key. Throws if eitherpubJwtKeyorsubJwtKeyare passed.pubJwtKey(String, required) : the publisher's jwt key. Throws ifjwtKeyis also passed.subJwtKey(String, required) : the subscriber's jwt key. Throws ifjwtKeyis also passed.path(String, defaults to'/.well-known/mercure') : the hub's route.allowAnonymous(Boolean, defaults tofalse) : set totrueto allow subscribers with no valid JWT to connect.maxTopics(Number, defaults to0) : maximum topics count the subscribers can subscribe to.0means no limit.ignorePublisherId(Boolean, defaults totrue) : set tofalseto accept the event ID by the publisher instead of creating a new one.publishAllowedOrigins(Array<String>, defaults to[]) : a list of origins allowed to publish (only applicable when using cookie-based auth).redis(Object, optional) : if defined, the Hub will connect to a Redis instance and use it to store the events and scale across multiple instances. This option is directly passed to theredis.createInstance()method of theredisnpm module.
Returns : a new Hub instance.
Listens to incoming subscription requests. It can be stopped with the methods Hub#end() or Hub#endSync().
Arguments :
port(Number, required) : the port which the hub will listen to.addr(String, defaults to'0.0.0.0') : the listening bound address.
Returns : a Promise resolving when the server has started listening.
Sends an update to the subscribers. Only the subscribers watching the corresponding topics will receive the update if they are allowed to.
Arguments :
topics(Array<String> || String, required) : Topic(s) of the update.data(String, required) : the message to send to the subscribers.opts(Object, optional) :id(String, optional) : the event ID. Will be discarded ifignorePublisherIdis set totruein the hub's configuration.targets(Array<String>, defaults to[]) :allTargets(Boolean, defaults tofalse) :type(String, defaults to'message') : the event message type.retry(Number, defaults to0) : the subscriber's reconnection time.
Returns : a Promise resolving with the update ID when the update has been dispatched.
Generates a JWT using the stored JSON Web Token key. This JWT contains the targets the subscriber is allowed to get updates about.
Arguments :
claims(Object, required) :publish: (Array<String>, optional) : targets that the client can publish to.subscribe: (Array<String>, optional) : targets that the client can subscribe to.
Returns : a Promise resolving a String containing the JWT.
Generates a JWT using the stored JSON Web Token key. This JWT only contains permissions to publish on the given targets.
Arguments :
targets: (Array<String>, optional) : targets that the client can publish to.
Returns : a Promise resolving a String containing the JWT.
Generates a JWT using the stored JSON Web Token key. This JWT only contains permissions to subscribe on the given targets.
Arguments :
targets: (Array<String>, optional) : targets that the client can subscribe to.
Returns : a Promise resolving a String containing the JWT.
Extracts the claims from a request (header or cookie) addressed to the hub. This only extracts claims from a request sent by a publisher.
Arguments :
req(http.ClientRequest) :
Returns : a Promise resolving the claims extracted from the request's JWT.
Extracts the claims from a request (header or cookie) addressed to the hub. This only extracts claims from a request sent by a subscriber.
Arguments :
req(http.ClientRequest) :
Returns : a Promise resolving the claims extracted from the request's JWT.
Gracefully stops the hub asynchronously. This will close the connections to the subscribers.
Arguments :
opts(Object, optional) :force(Boolean, defaults tofalse) : set totrueto forcefully close all connections.
Returns : a Promise resolving when the hub is stopped.
Forcefully stops the hub synchronously.
Arguments : (none)
Returns : (void)
Changes the JSON Web Token key. This will close all subscribers' open connections, except the ones from subscribers who have full subscription rights to the hub.
Arguments : (none)
Returns : a Promise resolving when the JWT has been changed.
Will :
- generate a new JWT key.
- close all subscribers' open connections, except the ones from subscribers who have full subscription rights to the hub.
- outputs the new JWT Key to stdout.
Use case : in case the hub must urgently close all connections (e.g.: in case of compromission of the JWT key).
Arguments : (none)
Returns : a Promise resolving when the JWT has been changed.
Initializes a new Server instance. When clustering the application, it is required to connect the server to Redis in order to leverage its pub/sub capabilities and scale across multiple hub instances.
The instance does not immediately "listen". Calling the Server#listen() method is required.
Arguments :
Note : this constructor takes the same options as Hub#constructor().
server(http.Server, optional) : a nativehttp.Serverinstance. If not passed, the hub will create one.config(Object, optional) :id(String, optional) : a unique ID identifying this instance amongst a full instances cluster.jwtKey(String, required) : the publisher's AND subscriber's JSON Web Token key. Throws if eitherpubJwtKeyorsubJwtKeyare passed.pubJwtKey(String, required) : the publisher's jwt key. Throws ifjwtKeyis also passed.subJwtKey(String, required) : the subscriber's jwt key. Throws ifjwtKeyis also passed.path(String, defaults to'/.well-known/mercure') : the hub's route.allowAnonymous(Boolean, defaults tofalse) : set totrueto allow subscribers with no valid JWT to connect.maxTopics(Number, defaults to0) : maximum topics count the subscribers can subscribe to.0means no limit.ignorePublisherId(Boolean, defaults totrue) : set tofalseto accept the event ID by the publisher instead of creating a new one.publishAllowedOrigins(Array<String>, defaults to[]) : a list of origins allowed to publish (only applicable when using cookie-based auth).redis(Object, optional) : if defined, the Hub will connect to a Redis instance and use it to store the events and scale across multiple instances. This option is directly passed to theredis.createInstance()method of theredisnpm module.
Returns : a new Server instance.
Creates a http.Server instance and a Hub from an existing Express app. The created hub is bound to the http server.
The http.Server instance does not immediately "listen". Calling the http.Server#listen() method is required.
Note : the Hub requires data encoded to x-form-urlencoded, thus the Express app MUST use the express.urlencoded() middleware beforehand, in order to parse the requests datas.
Arguments :
app: the Express application.config(Object) : the configuration to pass to the Hub. SeeHub#constructor().
Returns : an Object :
server(http.Serverinstance) : the created http server from the Express app.hub(Hubinstance) : the Hub that will handle the SSE connections.
Listens to incoming subscription requests. It can be stopped with the methods Hub#end() or Hub#endSync().
Arguments :
port(Number, required) : the port which the hub will listen to.addr(String, defaults to'0.0.0.0') : the listening bound address.
Returns : a Promise resolving when the server has started listening.
Gracefully stops the hub asynchronously. This will close the connections to the subscribers.
Arguments :
opts(Object, optional) :force(Boolean, defaults tofalse) : set totrueto forcefully close all connections.
Returns : a Promise resolving when the hub is stopped.
Forcefully stops the hub synchronously.
Arguments : (none)
Returns : (void)
Initializes a new publisher, ready to send messages to its hub.
Arguments :
- The 1st and only argument is required. It is either :
- a
Hubinstance (for publisher that are located in the same code base as the hub). - a configuration
Object: (usually for remote publishers)protocol(String, defaults to'https')host(String, required)port(Number, defaults to80)path(String, defaults to'/.well-known/mercure')jwt(String, required)rsaPrivateKey(String, optional) :
- a
Sends a message to the hub. The hub will dispatch the event to the appropriate subscribers.
Arguments :
topics(Array<String> || String, required) : the topics of the publication.message(String, required) : The content to send to the subscribers.options(Object, optional) :targets(Array<String> || String, optional) : the targets that will receive the update. Passing nothing will publish the update to all subscribers.id(String, optional) : the ID to give to the sent update. The server can discard it if it's configured to do so.type(String, defaults to'message') : the type of the event to send to the subscribers.retry(Number, optional) : the reconnection cooldown to send to the subscribers.allTargets(Boolean, optional) : set totrueto dispatch the update to all subscribers. Only available when the publisher is directly linked to the Hub instance.
Returns : a Promise resolving the event ID when the message has been sent to the hub.
Allows encryption of the sent update to the Hub.
Arguments :
config(Object, optional)rsaPrivateKey(String, optional) : the private RSA key that will be used to cypher the messages. If nothing is passed, the method will use the key passed in the class constructor. If nothing was passed, the method generates a new RSA public/private key pair.
Returns : a Promise resolving an Object when the encryption mechanism is ready :
rsaPrivateKey(String) : the used RSA private key.rsaPublicKey(String) : the public RSA key, calculated from the private RSA key.
Decodes the stored JWT used to send updates. If no JWT is used (e.g.: the Publisher is created with a Hub instance), returns null.
Arguments : (none)
Returns : an Object containing the decoded JWT's payload.
