From 2f4e084b4f8280a884b03ae7d1f00b6f5b1a62d2 Mon Sep 17 00:00:00 2001 From: David Starke Date: Tue, 5 Jul 2011 13:11:01 -0700 Subject: [PATCH] Allow publishing and subscribing to tagged messages --- README.md | 10 ++++++++++ lib/hub.js | 10 +++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4c9afcf..3106833 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,16 @@ or we can use double asterisk for recursive wildcard, to subscribe to everything hub.subscribe("**", listenerFunction); +In addition, we can use tagged subscriptions to subscribe to a subset of tagged messages on a channel: + + hub.subscribe("name/of/*:tagname", listenerFunction); + +Using tags requires that objects are published with a tag: + + hub.publish("name/of/channel:tagname", {foo:"bar"}); + +Messages that are published with a tag will be sent to all subscribers that are subscribed to a matching tag, or that are subscribed to the base channel without a tag. + Tunguska also supports named event sub-types within each channel. The subscribe function takes an optional second parameter for specifying a specific event type to listen for. For example, diff --git a/lib/hub.js b/lib/hub.js index a921bbb..8b7dff9 100644 --- a/lib/hub.js +++ b/lib/hub.js @@ -28,7 +28,15 @@ exports.publish= function(channel, message){ var publishedToClients = {}; notifyAll(channel); var index = channel.lastIndexOf("/"); - channel = channel.substring(0, index + 1); + var tagSeparatorIndex = channel.lastIndexOf(":"); + var pathPart = channel.substring(0, index + 1); + if(tagSeparatorIndex>index){ + var idPart = channel.substring(index+1, tagSeparatorIndex); + var tagPart = channel.substring(tagSeparatorIndex); + notifyAll(pathPart+idPart); + notifyAll(pathPart+"*"+tagPart); + } + channel = pathPart; notifyAll(channel + "*"); notifyAll(channel + "**"); while(channel){