EPCP library for NodeJS
HTTP Extensible Pubsub Control Protocol (EPCP) defines a generalized and extensible data publishing protocol using HTTP. Data is published by way of HTTP POST, whose content is the JSON string representation of an object that follows certain structural guidelines.
Each message consists of one or more data items, and is always sent to a specified channel. The messages are associated with that channel and will only be delivered to those listeners subscribed to the channel.
This library contains a Format base class and a PubControl class that is used to send messages. This library also supports the case when the EPCP endpoint requires certain types of authentication.
This example illustrates the process of instantiating the PubControl publisher class, defining a data format, and then publishing some data.
var util = require'util';var pubcontrol = require'pubcontrol';var thisbody = body; ;utilinheritsHttpResponseFormat pubcontrolFormat;return 'http-response'; ;return 'body': thisbody;varif successconsole.log'Publish successful!';elseconsole.log'Publish failed!';console.log'Message: ' + message;console.log'Context: ';consoledircontext;;// PubControl can be initialized with or without an endpoint configuration.// Each endpoint can include optional JWT authentication info.// Multiple endpoints can be included in a single configuration.// Initialize PubControl with a single endpoint:var pub ='uri': '<myrealm>''iss': '<myrealm>''key': '<myrealmkey' 'base64';// Add new endpoints by applying an endpoint configuration:pubapplyConfig'uri': '<myendpoint_uri_1>''uri': '<myendpoint_uri_2>';// Remove all configured endpoints:pubremoveAllClients;// Explicitly add an endpoint as a PubControlClient instance:var pubclient = '<myendpoint_uri>';// Optionally set JWT auth: pubclient.setAuthJwt(<claim>, '<key>');// Optionally set basic auth: pubclient.setAuthBasic('<user>', '<password>');pubaddClientpubclient;// Publish across all configured endpoints:pubpublish'<channel>''Test Publish!' callback;
In some cases, the EPCP endpoint requires authentication before allowing its use. This library can provide Basic and JWT authentication for these cases. To use Basic authentication instantiate a PubControlClient class, use setBasicAuth() to set the username and password, and add the PubControlClient instance to the PubControl instance via addClient() as shown in the example above. To use JWT authentication pass a configuration to PubControl when instantiating it or via applyConfig and provide the claim as shown in the example above.
If the claim does not contain an exp value, then this library will create an appropriate value for that field on each use. Since the header is generated from the authorization object each time it needs to be used, the library is able to generate a new authorization header, even from the same auth object.
It is also possible to use a literal JWT string for JWT authentication. This may be useful in certain cases, such as when you are performing a push request on behalf of another service. That service can preencode the JWT token and hand it to you in its string representation. This way, that service does not need to hand the JWT signing key to you.
pubclientsetAuthJwt'######.######.######'; // Literal JWT string
(C) 2015 Fanout, Inc.
Licensed under the MIT License, see file COPYING for details.