node.js - Redis pub/sub - same process listening to one channel -


i have single node.js server - process listen messages sent - testing only. problem having when publishing message same process, subscriber doesn't seem receive @ all.

i have setup:

var redis = require('redis');  var rcpub = redis.createclient(); var rcsub = redis.createclient();  var message = string('testing123');  rcsub.subscribe('redis_channel@test_overall_health');  rcsub.on('message', function (channel, msgs) {      console.log(channel,msgs);  });   rcpub.publish('redis_channel@test_overall_health', message); 

i have 1 redis client acts subscriber , 1 publisher, way must it, reason messages aren't being received. there limitation process can't listen messages publishes? doesn't seem make sense. can verify code more or less right because other processes listening same channel received message.

apparently, subscribe command being sent after publish command.

node's redis client queues commands until connection established redis server , flushes queued commands server when connect event received on socket. client initiated connection first (publisher), receive connect event first, @ point send queued commands (publish). because redis processes commands in single thread, subscriber subscribes after publish command complete. other processes able receive messages since they've subscribed channel.

creating subscriber client first should work in cases, albeit safer approach wait subscription complete before publishing messages:

var redis = require('redis');  var publisher = redis.createclient(),     subscriber = redis.createclient(),     message = 'testing123';  subscriber.subscribe('redis_channel@test_overall_health');  subscriber.on('message', function (channel, message) {     console.log(channel, message); });  subscriber.on('subscribe', function (channel, count) {     publisher.publish('redis_channel@test_overall_health', message); }); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -