centro-js

4.0.0

centro-js

Pub-sub and work queue server. Wildcards, streams, back-pressure, multi-transport. Just Node and a filesystem required.

Re-exports all exports of centro-js/lib/client and centro-js/lib/server

centro-js

Client

centro-js/lib/client

Centro client functions

centro-js/lib/client
Static Members
separate_auth(config?, cb)
stream_auth(stream, config?)
authzCallback(err, userpass, cb)
streamCallback(stream)
Events
MQlobberClient#ready

Server

centro-js/lib/server

Centro server class

centro-js/lib/server
Static Members
closeCallback(err)
cancelCallback(err)
oncloseCallback(cb)
destroyCallback()
Events
CentroServer#ready
CentroServer#transport_ready
CentroServer#authz_start
CentroServer#authz_end
CentroServer#pre_connect
CentroServer#connect
CentroServer#disconnect
CentroServer#expired
CentroServer#empty
CentroServer#close
CentroServer#error
CentroServer#warning

Transports

centro-js/lib/server_transports/primus

Primus transport. This allows messages to be sent over Primus connections.

centro-js/lib/server_transports/primus
Parameters
config (Object) Configuration options. This supports all the options supported by Primus.createServer and PrimusDuplex as well as the following:
Name Description
config.server Primus? If you want to supply your own Primus server object. Otherwise, Primus.createServer will be called to create one.
config.pathname string (default /centro/v2/primus) Pathname prefix on which Primus listens for connections.
config.primus_transport Object? Primus transformer-specific configuration.
config.primus Object? If present then this is used in preference to config .

centro-js/lib/server_transports/tcp

TCP transport. This allows messages to be sent over TCP connections.

centro-js/lib/server_transports/tcp
Parameters
config (Object) Configuration options. This supports all the options supported by net.Server#listen , net.createServer and tls.createServer as well as the following:
Name Description
config.server (net.Server | tls.Server)? If you want to supply your own TCP or TLS server object. Otherwise, net.createServer or tls.createServer will be called to create one.
config.noDelay boolean? If present then setNoDelay is called on every connection with this as the argument.
config.tcp Object? If present then this is used in preference to config .

centro-js/lib/server_transports/http

HTTP transport. This allows messages to be published using HTTP POST requests and received using HTTP Server-Sent Events.

Publish messages using POST requests of the form:

/centro/v2/publish?authz_token=XXX&topic=YYY
  • XXX is a JSON Web Token allowing access to the server. See here for more information about Centro authorization tokens.
  • YYY is the message's topic. Topics should be in AMQP format: . delimits words.
  • The request body should contain the message's data. The server doesn't interpret the data, it just treats it as a binary stream.

Subscribe to messages using GET requests of the form:

/centro/v2/subscribe?authz_token=XXX&topic=YYY&topic=ZZZ
  • XXX is the authorization token allowing access to the server.
  • YYY is the message topic to which to subscribe. Topics should be in AMQP format: . delimits words, * matches exactly one word and # matches zero or more words.
  • You can specify more than one topic, e.g. ZZZ above.

Messages you've subscribed to are delivered using server-sent events. Each message begins with a start event, continues with multiple data events and finishes with an end event.

For example, consider a message with topic foo.bar and body wow. You could receive the following events:

type: start
data: {"id":0,"single":false,"existing":false,"expires":1498375188,"size":3,"topic":"foo.bar"}

type: data
data: {"id":0,"data":"wow"}

type: end
data: {"id":0}

For larger messages you may receive multiple data messages. The event data for each event is JSON-encoded and always contains a message id (in this case 0). If you're receiving multiple messages at the same time, their events may be interleaved so you need to use the id to tell which event is for which message. The id identifies the message for this connection only - it isn't globally unique and the same message may have a different ids on different connections.

In the start event, you'll receive whether the messages is being delivered to a single subscriber, whether it's an existing message (i.e. published before we subscribed), when it expires (in seconds since 1970-01-01), the size of its body data and its topic.

In the data event, you'll receive (part of) the message's body (wow in this case). Since this is JSON-encoded, the data will be a UTF-8 encoded string, even for binary data. If you need to get the raw binary data, encode the string as latin-1 and use the bytes in the result (the server decodes the bytes as latin-1 before JSON encoding the result).

Note that server-sent event IDs and the Last-Event-ID header are not supported because when a connection drops, the server forgets all about it.

centro-js/lib/server_transports/http
Parameters
config (Object) Configuration options. This supports all the options supported by net.Server#listen and https.createServer as well as the following:
Name Description
config.server (http.Server | https.Server)? If you want to supply your own HTTP or HTTPS server object. Otherwise, http.createServer or https.createServer will be called to create one.
config.pathname string (default /centro/v2/) Pathname prefix on which to listen for requests. Publish requests are made on /centro/v2/publish and subscribe requests are made on /centro/v2/subscribe .
config.sse_keep_alive_interval integer? If present, the number of seconds between sending a Server-Sent Event comment in order to keep a connection alive. If you don't specify this, no keep-alive comments will be sent.
config.destroy_after_end_delay integer (default 500) Amount of time (in milliseconds) to wait between end ing a response and destroy ing the connection, if an error occurs which means the connection can't be re-used. If you don't want the connection forcibly destroyed, set this to a negative value.
config.http Object? If present then this is used in preference to config .
config.access Object? Passed to access-control .
config.http2 boolean? Whether to use HTTP/2 ( http2.createServer / http2.createSecureServer ) instead of HTTP. Note this still uses Server-Sent Events to deliver messages. If you want to use HTTP/2 streams to deliver messages, see the http2 and http2-duplex transports. Defaults to false .

centro-js/lib/server_transports/http2

HTTP/2 transport. This allows messages to be sent over HTTP/2 streams.

Note that this transport won't work with browser clients. You'll need to use the http2-duplex transport with browsers.

centro-js/lib/server_transports/http2
Parameters
config (Object) Configuration options. This supports all the options supported by http2.createServer , http2.createSecureServer and net.Server#listen as well as the following:
Name Description
config.server (http2.Http2Server | http2.Http2SecureServer)? If you want to supply your own HTTP/2 server object. Otherwise, http2.createServer or { https://nodejs.org/api/http2.html#http2_http2_createsecureserver_options_onrequesthandler|http2.createSecureServer} will be called to create one.
config.pathname string (default /centro/v2/http2) Pathname prefix on which to listen for requests.
config.http2 Object? If present then this is used in preference to config .
config.access Object? Passed to access-control .

centro-js/lib/server_transports/http2-duplex

HTTP/2 transport for browsers. This allows messages to be sent over HTTP/2 streams to browsers.

If your client isn't a browser, you should be able to use the http2 transport.

This transport uses the http2-duplex module to emulate a full-duplex connection with browsers. Browser-to-server streaming isn't implemented by any browser, nor are there any plans to do so. http2-duplex emulates it by using POST requests.

centro-js/lib/server_transports/http2-duplex
Parameters
config (Object) Configuration options. This supports all the options supported by http2.createServer , http2.createSecureServer , net.Server#listen and http2-duplex as well as the following:
Name Description
config.server centro-js/lib/server_transports/http2-duplex.CentroHttp2DuplexServer? If you want to supply your own HTTP/2 full-duplex emulation server. This class inherits from Http2DuplexServer in http2-duplex . If you don't supply an instance (the default) then one will be created for you, along with an instance of Http2Server or Http2SecureServer .
config.pathname string (default /centro/v2/http2-duplex) Pathname prefix on which to listen for requests.
config.http2_duplex Object? If present then this is used in preference to config .
config.access Object? Passed to access-control .

centro-js/lib/server_transports/in-mem

In-memory transport. This allows messages to be sent over an in-memory stream to a server running in the same process. The in-memory stream has minimal overhead (the messages aren't copied).

An extra transport operation for this transport is added to the server object:

Name Type
connect connectCallback
centro-js/lib/server_transports/in-mem
Parameters
config (Object) Configuration options. This supports all the options supported by stream.Duplex as well as the following:
Name Description
config.in-mem Object? If present then this is used in preference to config .
Static Members
connectCallback(err, stream)

Extensions

CentroServer

Create a server which publishes and subscribes to messages on behalf of clients.

new CentroServer(config: Object)

Extends events.EventEmitter

Parameters
config (Object) Configuration options. This supports all the options supported by MQlobberServer and AccessControl as well as the following:
Name Description
config.transport (string | Object)? Specifies which transport to load into the server. This should be either the transport's name or its configuration, with its name in the server property.
config.transports Array<(string | Object)>? Specifies multiple transports to load into the server.
config.authorize Function? Function for creating an object which can authorize JSON Web Tokens presented by clients. Defaults to authorize-jwt . If you supply your own, it must comply with authorize-jwt's API. authorize_config or config is passed as the first argument.
config.authorize_config Object? Configuration specific to config.authorize .
config.allowed_algs Array<string> Which JWT algorithms are allowed. Clients which present tokens which aren't signed with one of these algorithms are rejected. You must supply this list.
config.privileged boolean (default false) If true, clients can see messages from all token issuers. If false (the default), a client can only see messages sent by another client if they were both authorized using a token with the same issuer. You can make clients on individual transports privileged by setting this property in their transport-specific config ( config.transports[X].config.privileged ).
config.fsq (QlobberFSQ | QlobberPG)? QlobberFSQ or QlobberPG instance for handling messages. Defaults to a new instance of QlobberFSQ , passing fsq_config or config to the constructor.
config.fsq_config Object? Configuration specific to config.fsq .
config.realm string (default centro) When authorization of a client fails, the transport is given information it can (optionally) use when rejecting the connection. This includes a HTTP authentication header specifying this realm.
config.auth_method string (default Bearer) When authorization of a client fails, the transport is given information it can (optionally) use when rejecting the connection. This includes a HTTP authentication header specifying this authentication method. Note that the client can opt to use Bearer or Basic authentication regardless of this setting.
config.max_tokens integer (default 10) Maximum number of authorization tokens each client is allowed to present.
config.max_token_length integer (default 1MiB) Maximum size of authorization token each client is allowed to present.
config.max_topic_length integer (default 4KiB) Maximum length of topics the client can specify, in subscription and publish requests and in the authorization token.
config.max_issuer_length integer (default 128) Maximum length of the iss claim in authorization tokens.
config.max_subscribe_topics integer (default 1000) Maximum number of topics that subscribe claims in authorization tokens can contain. These claims specify to which topics client which present them are pre-subscribed.
config.max_allow_publish_topics integer (default 1000) Maximum number of topics that access_control.publish.allow claims in authorization tokens can contain. These claims are passed to AccessControl and specify to which topics clients which present them can publish messages.
config.max_disallow_publish_topics integer (default 1000) Maximum number of topics that access_control.publish.disallow claims in authorization tokens can contain. These claims are passed to AccessControl and specify to which topics clients which present them cannot publish messages.
config.max_allow_subscribe_topics integer (default 1000) Maximum number of topics that access_control.subscribe.allow claims in authorization tokens can contain. These claims are passed to AccessControl and specify to which topics clients which present them cannot subscribe.
config.max_disallow_subscribe_topics integer (default 1000) Maximum number of topics that access_control.subscribe.disallow claims in authorization tokens can contain. These claims are passed to AccessControl and specify to which topics clients which present them cannot subscribe.
config.max_block_topics integer (default 1000) Maximum number of topics that access_control.block claims in authorization tokens can contain. These claims are passed to AccessControl and specify which messages aren't delivered to clients which present them.
config.max_presence_data_length integer (default 1MiB) Maximum length of presence.connect.data and presence.disconnect.data claims in authorization tokens can contain. The presence claims specify messages to send when clients which present them connect and disconnect.
Instance Members
fsq
transport_ops
close(cb)
attach_extension(ext, config?)

centro-js/lib/server_extensions/backoff

Centro extension for defining backoff event behaviour.

centro-js/lib/server_extensions/backoff
Static Members
backoff(config)
skipCallback(info)
delayCallback(info)

centro-js/lib/server_extensions/close_conn_on_error

Centro extension for closing a connection when an error occurs on it.

centro-js/lib/server_extensions/close_conn_on_error
Static Members
close_conn_on_error()

centro-js/lib/server_extensions/filter

Centro extensions for filtering messages.

centro-js/lib/server_extensions/filter
Static Members
delay_message_until_all_streams_under_hwm(config)
fastest_writable(config?)
dummy_data_event()
delayCallback(info)
fastestWritableCallback(fw, mqserver, stream, info)

centro-js/lib/server_extensions/full

Centro extension for defining full event behaviour.

centro-js/lib/server_extensions/full
Static Members
full(config)
skipCallback(info)
delayCallback(info)

centro-js/lib/server_extensions/limit_active

Centro extension for limiting the number of active subscriptions, publications and connections.

centro-js/lib/server_extensions/limit_active
Static Members
limit_active_subscriptions(config)
limit_active_publications(config)
limit_active_connections(config)

centro-js/lib/server_extensions/limit_conn

Centro extension for limiting the total amount of data published and the total number of messages published by a connected client.

centro-js/lib/server_extensions/limit_conn
Static Members
limit_conn_published_data(config)
limit_conn_published_messages(config)

centro-js/lib/server_extensions/limit_transport

Centro extension for limiting the number of concurrent connections each transport supports.

centro-js/lib/server_extensions/limit_transport
Static Members
limit_transport_connections(config)

centro-js/lib/server_extensions/throttle

Centro extension for limiting the rate of messages streams.

centro-js/lib/server_extensions/throttle
Static Members
throttle_publish_streams(config)
throttle_message_streams(config)

centro-js/lib/server_extensions/timeout

Centro extension for limiting how long message streams can be open.

centro-js/lib/server_extensions/timeout
Static Members
timeout_publish_streams(config)
timeout_message_streams(config)
timeout_http_publish_requests(config)