Skip to content

Instantly share code, notes, and snippets.

@zoalasaurusrawr
Created September 4, 2013 19:51
Show Gist options
  • Select an option

  • Save zoalasaurusrawr/6441986 to your computer and use it in GitHub Desktop.

Select an option

Save zoalasaurusrawr/6441986 to your computer and use it in GitHub Desktop.
Simple node.js pub/sub
/*
* This is a very simple node.js pub/sub system.
*
* Subscribers can subscribe with a valid http endpoint that accepts
* a POST. When /publish is called on this API, it will publish the
* body to each subscriber.
*
* == Required Node Modules ==
* express, request, node-cache
*/
var express = require('express');
var app = express();
var nodeCache = require('node-cache');
var cache = new nodeCache();
var request = require('request');
/*
* api variables
*/
var subscriptions = [];
/*
* Tells express that we want to part json
*/
app.configure(function() {
console.log('configuring...');
app.use(express.bodyParser()); // used to parse JSON object given in the request body
console.log('configuration complete!');
});
/*
* Returns the version of this api
*/
app.get('/version', function(req, res) {
console.log('client asked for version');
res.type('text/plain');
res.send('Brokerage Data Fabric v1.0');
});
/*
* Future implementation - works but the cache isn't used.
* This will return cache statis for node-cache
*/
app.get('/admin/cache', function(req, res) {
console.log('client asked for cache statistics');
res.type('text/plain');
res.send(cache.getStats());
});
/*
* Returns a plaint text string of all the subscribers
*/
app.get('/admin/subscriptions', function(req, res) {
console.log('client asked for subscription statistics');
res.type('text/plain');
var length = subscriptions.length;
console.log(length + ' active subscriptions found');
var subscriptionString = "Active Subscriptions: \r";
for (var i = 0; i < length; i++){
console.log('subscription found for ' + subscriptions[i]);
subscriptionString += subscriptions[i].uri + "\r";
}
res.send(subscriptionString);
});
/*
Fiddler test header
Host: localhost:1635
Content-Length: 44
Content-Type: application/json
Fiddler test body
{
"uri": "http://localhost:1635/bdf"
}
*/
/*
* Call this method with a JSON object containing an endpoint that
* the publisher can post to. All content must be application/json
*/
app.post('/subscribe', function(request, response) {
var uriInfo = request.body; //You have to assign the body to a javascript object before use
//Check that the subscription doesn't exist yet
var length = subscriptions.length;
console.log(length + ' active subscriptions found');
var subscriptionFound = false;
for (var i = 0; i < length; i++){
if (subscriptions[i].uri == uriInfo.uri){
console.log('subscription already found for uri: ' + uriInfo.uri);
subscriptionFound = true;
}
}
//If we never hit the flag for a subscription, add it
if (!subscriptionFound){
subscriptions[subscriptions.length] = uriInfo;
console.log('subscription added for ' + uriInfo.uri + "@ " + request.connection.remoteAddress);
var subscription = new Object();
subscription.status = "Success";
subscription.message = "Subscription was successfully created";
response.type('application/json');
response.send(subscription);
}else{
var subscription = new Object();
subscription.status = "No Action";
subscription.message = "Subscription already exists";
response.type('application/json');
response.send(subscription);
}
});
/*
* bdf - sample endpoint for the test publisher to talk back to
* you still need to subscribe with localhost:post/bdf as the
* subsriber in order to have this work
*/
app.post('/bdf', function(request, response) {
var message = request.body; //You have to assign the body to a javascript object before use
console.log('received published message: ' + message.message);
var subscription = new Object();
subscription.status = "Success";
subscription.message = "Message Received";
response.type('application/json');
response.send(subscription);
});
/* Fiddler Test Body For Publishing
{
"message": "test message"
}
*/
/*
* publish - takes a json body from an http post
* and sends it out to subscribers
*/
app.post('/publish', function(request, response){
var length = subscriptions.length;
var message = request.body;
for (var i = 0; i < length; i++){
console.log('publishing message to: ' + subscriptions[i].uri);
var request = require("request");
request({
url: subscriptions[i].uri,
method: "POST",
json: message
}, function _callback(err, res, body) {
var result = body;
});
}
var subscription = new Object();
subscription.status = "Success";
subscription.message = "Message Published";
response.type('application/json');
response.send(subscription);
});
app.listen('1635');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment