express-grip
Author: Konstantin Bokarius kon@fanout.io
An Express GRIP library.
License
express-grip is offered under the MIT license. See the LICENSE file.
Installation
This library is compatible with both Express 3 and 4.
npm install express-grip
Usage
This library comes with two middleware classes which you must use. Express-grip performs its magic and provides conveniences through the use of both pre-route and post-route middleware. Therefore, even if a particular route does not use express-grip features, it's necessary to call next() at the end of your route handler to ensure that the post-route middleware executes. The middleware will parse the Grip-Sig header in any requests to detect if they came from a GRIP proxy, and it will apply any hold instructions when responding. Additionally, the middleware handles WebSocket-Over-HTTP processing so that WebSockets managed by the GRIP proxy can be controlled via HTTP responses from the Express application.
Configure express-grip by providing a configuration object of your choice to the configure() method. The various express-grip settings are shown below. Note that your configuration object should provide access to the express-grip settings via dot notation. Call the configure() method in your app.js file like so:
var expressGrip = ; var myConfigObject = ... ; expressGrip;
Set gripProxies for GRIP proxy validation and publishing:
// Pushpin and/or Fanout.io is used for sending realtime data to clientsvar myConfigObject = gripProxies = // Pushpin 'control_uri': 'http://localhost:5561' 'key': 'changeme' // Fanout.io 'control_uri': 'https://api.fanout.io/realm/your-realm' 'control_iss': 'your-realm' 'key': Base64 ...;
If it's possible for clients to access the Express app directly, without necessarily going through the GRIP proxy, then you may want to avoid sending GRIP instructions to those clients. An easy way to achieve this is with the gripProxyRequired setting. If set, then any direct requests that trigger a GRIP instruction response will be given a 501 Not Implemented error instead.
var myConfigObject = gripProxyRequired: true ...;
To prepend a fixed string to all channels used for publishing and subscribing, set gripPrefix in your configuration:
var myConfigObject = gripPrefix = '<prefix>' ...;
You can also set any other EPCP servers that aren't necessarily proxies with publishServers:
var myConfigObject = gripPubServers = 'uri': 'http://example.com/base-uri' 'iss': 'your-iss' 'key': 'your-key' ...;
Express 4 example route:
var express = ;var router = express;var grip = ;var expressGrip = ; // Add the pre-handler middleware to the front of the stackrouter; router; router; // Add the post-handler middleware to the back of the stackrouter; moduleexports = router;
Express 4 stateless WebSocket echo service example with broadcast endpoint:
var express = ;var router = express;var bodyParser = ;var grip = ;var expressGrip = ; // Add the pre-handler middleware to the front of the stackrouter; routerall'/websocket' { // Reject non-WebSocket requests if !expressGrip return; // If this is a new connection, accept it and subscribe it to a channel ws = expressGrip; if ws wsaccept; ws; while ws // Note that recv() will always return a String while recvRaw() can be // used to get either a String or Buffer depending on whether the // message is TEXT or BINARY respectively var message = ws; // If return value is undefined then connection is closed if message == null ws; break; // Echo the message ws; // next() must be called for the post-handler middleware to execute ;}; router; // Add the post-handler middleware to the back of the stackrouter; moduleexports = router;