Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
Fixed missing message in RedisAscoltatore.
Browse files Browse the repository at this point in the history
Sending a message on topic 'aa' was not forwarded
with subscribers of 'aa/#'. This was due to some missing topic-rewriting
business which have been added.
  • Loading branch information
mcollina committed Apr 27, 2014
1 parent da34151 commit 7a438b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/behave_like_an_ascoltatore.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ module.exports = function() {
});
});

it("should support multi-level wildcard at the end of a topic with no separator", function(done) {
var that = this;
that.instance.sub("hello/*", wrap(done), function() {
that.instance.pub("hello");
});
});

it("should support single-level wildcard at start of topic", function(done) {
var that = this;
that.instance.sub("+/hello", wrap(done), function() {
Expand Down
23 changes: 21 additions & 2 deletions lib/redis_ascoltatore.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,13 @@ RedisAscoltatore.prototype._startSub = function() {
// we need to skip out this callback, so we do not
// break the client when an exception occurs
var ascoltatore = that._ascoltatores[sub];
topic = topic.replace(new RegExp('/$'), '');
topic = that._recvTopic(topic);

if (ascoltatore) {
ascoltatore.publish(that._recvTopic(topic), message);
ascoltatore.publish(topic, message);
} else {
console.log("no ascoltatore for subscription: " + sub);
}
});
};
Expand Down Expand Up @@ -170,12 +174,17 @@ RedisAscoltatore.prototype.subscribe = function subscribe(topic, callback, done)

var subTopic = this._subTopic(topic);

if (!subTopic.match(new RegExp('[/*]$'))) {
subTopic += '/';
}

if (this._containsWildcard(topic)) {
this._sub.psubscribe(subTopic, newDone);
} else {
this._sub.subscribe(subTopic, newDone);
}

debug("redis subscription topic is " + subTopic);
this._subs_counter.add(subTopic);

var ascoltatore = this._ascoltatores[subTopic];
Expand All @@ -198,7 +207,13 @@ RedisAscoltatore.prototype.publish = function publish(topic, message, done) {
message = false; // so we can convert it to JSON
}

this._client.publish(this._pubTopic(topic), message, function() {
topic = this._pubTopic(topic);

if (!topic.match(new RegExp('/$'))) {
topic += '/';
}

this._client.publish(topic, message, function() {
debug("new message published to " + topic);
util.defer(done);
});
Expand All @@ -210,6 +225,10 @@ RedisAscoltatore.prototype.unsubscribe = function unsubscribe(topic, callback, d
var isWildcard = this._containsWildcard(topic),
subTopic = this._subTopic(topic);

if (!subTopic.match(new RegExp('[/*]$'))) {
subTopic += '/';
}

this._subs_counter.remove(subTopic);

var ascoltatore = this._ascoltatores[subTopic];
Expand Down

0 comments on commit 7a438b7

Please sign in to comment.