GRIP Interface Library for NodeJS
Authors: Katsuyuki Ohmuro harmony7@pex2.jp, Konstantin Bokarius kon@fanout.io
Description
A GRIP interface library for NodeJS. For use with HTTP reverse proxy servers that support the GRIP interface, such as Pushpin.
Requirements
pubcontrol
Sample Usage
Examples for how to publish HTTP response and HTTP stream messages to GRIP proxy endpoints via the GripPubControl class.
var pubcontrol = ;var grip = ; var { if success console; else console; console; console; consoledircontext; }; // GripPubControl 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. var grippub = 'control_uri': 'https://api.fanout.io/realm/<myrealm>' 'control_iss': '<myrealm>' 'key': '<myrealmkey>' 'base64'; // Add new endpoints by applying an endpoint configuration:grippub; // Remove all configured endpoints:grippub; // 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>');grippub; // Publish across all configured endpoints:grippub;grippub;
Validate the Grip-Sig request header from incoming GRIP messages. This ensures that the message was sent from a valid source and is not expired. Note that when using Fanout.io the key is the realm key, and when using Pushpin the key is configurable in Pushpin's settings.
var grip = ; var isValid = grip;
Long polling example via response headers. The client connects to a GRIP proxy over HTTP and the proxy forwards the request to the origin. The origin subscribes the client to a channel and instructs it to long poll via the response headers. Note that with the recent versions of Apache it's not possible to send a 304 response containing custom headers, in which case the response body should be used instead (next usage example below).
var http = ;var grip = ; http; console
Long polling example via response body. The client connects to a GRIP proxy over HTTP and the proxy forwards the request to the origin. The origin subscribes the client to a channel and instructs it to long poll via the response body.
var http = ;var grip = ; http; console
WebSocket example using nodejs-websocket. A client connects to a GRIP proxy via WebSockets and the proxy forward the request to the origin. The origin accepts the connection over a WebSocket and responds with a control message indicating that the client should be subscribed to a channel. Note that in order for the GRIP proxy to properly interpret the control messages, the origin must provide a 'grip' extension in the 'Sec-WebSocket-Extensions' header. To accomplish this with nodejs-websocket, edit Connection.js and ensure that the following header is appended to the 'this.socket.write()' function call in the answerHandshake() method: 'Sec-WebSocket-Extensions: grip; message-prefix=""\r\n' To accomplish this with ws, add the ws.on('headers', ...) check to your app, for example:
wss; ... server;
var ws = var pubcontrol = ;var grip = ; ws; console;
WebSocket over HTTP example. In this case, a client connects to a GRIP proxy via WebSockets and the GRIP proxy communicates with the origin via HTTP.
var http = ;var pubcontrol = ;var grip = ; http; console;
Parse a GRIP URI to extract the URI, ISS, and key values. The values will be returned in a dictionary containing 'control_uri', 'control_iss', and 'key' keys.
var grip = ;var config = grip;
License
(C) 2015 Fanout, Inc.
Licensed under the MIT License, see file COPYING for details.