Skip to content

Commit

Permalink
Updated simple PUBSUB example given that we no longer require one to …
Browse files Browse the repository at this point in the history
…wait for the client to connect to redis before issuing commands
  • Loading branch information
fictorial committed Apr 20, 2010
1 parent 9a601a5 commit 57dfb00
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
28 changes: 14 additions & 14 deletions examples/publisher.js
Expand Up @@ -2,20 +2,20 @@

// This script plays the role of publisher.

var sys = require("sys");
var redis = require("../lib/redis-client");

//redis.debugMode = true;
var client = redis.createClient();
var
sys = require("sys"),
client = require("../lib/redis-client").createClient();

// Publish a message once a second to a random channel.

client.stream.addListener("connect", function () {
setInterval(function () {
var channelName = "channel-" + Math.random().toString().substr(2);
var payload = "The time is " + (new Date());
client.publish(channelName, payload, function (err, reply) {
sys.puts("Published message to " + (reply === 0 ? "no one" : (reply + " subscriber(s).")));
});
}, 1000);
});
setInterval(function () {
var
channelName = "channel-" + Math.random().toString().substr(2),
payload = "The time is " + (new Date());

client.publish(channelName, payload,
function (err, reply) {
sys.puts("Published message to " +
(reply === 0 ? "no one" : (reply + " subscriber(s).")));
});
}, 1000);
20 changes: 9 additions & 11 deletions examples/subscriber.js
Expand Up @@ -3,15 +3,13 @@
// This script plays the role of listener/subscriber/consumer
// to **all** channels/classes.

var sys = require("sys");
var redis = require("../lib/redis-client");
var
sys = require("sys"),
client = require("../lib/redis-client").createClient();

//redis.debugMode = true;
var client = redis.createClient();

client.stream.addListener("connect", function () {
sys.puts("waiting for messages...");
client.subscribeTo("*", function (channel, message) {
sys.puts("[" + channel + "]: " + message);
});
});
sys.puts("waiting for messages...");

client.subscribeTo("*",
function (channel, message) {
sys.puts("[" + channel + "]: " + message);
});

0 comments on commit 57dfb00

Please sign in to comment.