PubControl for NodeJS
Authors: Katsuyuki Ohmuro harmony7@pex2.jp, Konstantin Bokarius kon@fanout.io
Description
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.
Requirements
jwt-simple
Sample Usage
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 HttpResponseFormat = function(body) { this.body = body; };
util.inherits(HttpResponseFormat, pubcontrol.Format);
HttpResponseFormat.prototype.name = function() { return 'http-response'; };
HttpResponseFormat.prototype.export = function() { return {'body': this.body}; }
var callback = function(success, message, context) {
if (success) {
console.log('Publish successful!');
}
else {
console.log('Publish failed!');
console.log('Message: ' + message);
console.log('Context: ');
console.dir(context);
}
};
var pub = new pubcontrol.PubControl({
'uri': 'https://api.fanout.io/realm/<myrealm>',
'iss': '<myrealm>',
'key': Buffer.from('<myrealmkey', 'base64')});
pub.applyConfig([{'uri': '<myendpoint_uri_1>'},
{'uri': '<myendpoint_uri_2>'}]);
pub.removeAllClients();
var pubclient = new pubcontrol.PubControlClient('<myendpoint_uri>');
pub.addClient(pubclient);
pub.publish('<channel>', new pubcontrol.Item(
new HttpResponseFormat('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.
pubclient.setAuthJwt('######.######.######');
Running pubcontrol
in web browsers
./etc/browser-demo
contains a demo of using webpack to build this library for the web browser, even running it in a separate service worker. The same service worker can be used with Cloudflare Workers as a way of building real-time serverless apps with fanout.io and Cloudflare Workers.
License
(C) 2015 Fanout, Inc.
Licensed under the MIT License, see file COPYING for details.