Skip to content

Instantly share code, notes, and snippets.

@bcg
Created December 15, 2010 23:40
Show Gist options
  • Save bcg/742787 to your computer and use it in GitHub Desktop.
Save bcg/742787 to your computer and use it in GitHub Desktop.
Semi functioning broker ... posting to show what writing your own broker might look like. The driver to follow after I clean it up a bit ...
#!/usr/bin/env node
var net = require('net');
var util = require('util');
var stomp = require('../lib/stomp');
var subscriptions = new stomp.Subscriptions();
var mqueues = new stomp.MemoryQueues();
net.createServer(function (stream) {
stomp.createBroker(stream, function(proto) {
proto.on('subscribe', function(client, queue) {
mqueues.register(queue);
subscriptions.add(queue, client);
// If you wanted all queues to route to *every* client you
// would do it here
mqueues.on('pop-'+queue, function() {
console.log("locally handled pop-"+queue);
});
});
proto.on('unsubscribe', function(client, queue) {
//subscriptions.rem(client, queue);
});
proto.on('send', function(queue, message) {
mqueues.push(queue, message);
});
});
// If you wanted to control which clients could get messages
// (round robin, etc).
mqueues.on('popable', function(queue, message) {
clients = subscriptions.find_clients_by_queue(queue);
if (clients.length >= 1) {
for (var i=0; i<clients.length; i++) {
clients[i].proto.message(queue, message);
}
}
});
}).listen(61613, '127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment