Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hierarchical Channel Subscriptions #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@

fanout.js - a simple and robust fanout pubsub
messaging server for node.js by @jazzychad
modifications by @realistschuckle

MOTIVATION
==========
MOTIVATION by @realistschuckle
==============================
I liked fanout.js but wanted hierarchichal subscriptions like the JMS
specification provides. This fork provides that.

MOTIVATION by @jazzychad
========================
I needed a robust and scalable event-driven message fanout server for several
Comet-based webapps where some process in the backend could send various
messages of different types/channels to the clients (browsers) in the front-end.
Expand Down Expand Up @@ -75,6 +81,19 @@ CLIENT PROTOCOL
If a client subscribes to a channel for which it is already subscribed,
the duplicate subscribe request is effectively ignored.

Example:

subscribe >

will subscribe to all messages

Example:

subscribe foo.bar.>

will subscribe to all messages published to any channel that begins with
"foo.bar.".

Unsubscribing
-------------
Client unsubscribe to a message channel by sending a message of the format:
Expand Down
27 changes: 23 additions & 4 deletions fanout.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,29 @@ var handleMessage = function handleMessage(conn, socket, data) {
}
};

var handleControllerMessage = function handleControllerMessage(socket, channel, data) {
msgEmitter.emit(channel, channel, data);
var handleControllerMessage = function handleControllerMessage(socket, channel, data, sourceChannel) {
msgEmitter.emit(channel, sourceChannel || channel, data);
};

function Channel(name) {
var parents = [],
nameParts = name.split('.');
for(var i = nameParts.length - 1; i >= 0; i -= 1) {
nameParts[i] = '>';
parents.push(nameParts.join('.'));
nameParts.splice(i, 1);
}

this.publish = function(socket, data) {
handleControllerMessage(socket, name, data);
parents.forEach(function(parentChannel) {
handleControllerMessage(socket, parentChannel, data, name);
});
}

return this;
}

function Client(connection) {
this.socket = connection;
this.name = null;
Expand Down Expand Up @@ -177,9 +196,9 @@ var controller = tcp.createServer(function(socket) {
for (var jj = 0; jj < dataarr.length-1; jj++) {
var dataline = dataarr[jj];
var i = dataline.indexOf(' ');
var channel = dataline.slice(0,i);
var channel = new Channel(dataline.slice(0,i));
var msg = dataline.slice(i+1);
handleControllerMessage(socket, channel, msg);
channel.publish(socket, msg);
}
});
});
Expand Down