Redis pub / sub for chat server in node.js

I am trying to use the Redis Cookbook example:

var http = require('http'), io = require('socket.io') fs = require('fs'), redis = require('redis'), rc = redis.createClient(9189, "pike.redistogo.com"); rc.auth("passwd", function() { console.log("Connected! to redistogo!");}); rc.on("connect", function() { rc.subscribe("chat"); console.log("rc connect event"); }); 

I succeed here, but never get a "message."

 rc.on("message", function (channel, message) { console.log("Sending: " + message); socketio.sockets.emit('message', message); }); webpage = http.createServer(function(req, res){ console.log('webpage request starting...'); fs.readFile('./index.htm', function(error, content) { if (error) { res.writeHead(500); res.end(); } else { res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(content, 'utf-8'); } }); }); webpage.listen(7777); 

my client side index.htm is

 <!docttype html> <html lang="en"> <head> <script src ="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"> </script> <script src="http://www.heaphash.com:7777/socket.io/socket.io.js"></script> <script> var socket = io.connect('www.heaphash.com', { port: 7777}); socket.on('message', function(data){ var li = new Element('li').insert(data); $('messages').insert({top: li}); } </script> <meta charset="utf-8"> <title>Chat with Redis</title> </head> <body> <ul id="messages"> <!-- chat messages go here --> </ul> <form id="chatform" action=""> <input id="chattext" type="text" value="" /> <input type="submit" value="Send" /> </form> <script> $('#chatform').submit(function(){ socket.emit('message', $('chattext').val()); $('chattext').val(""); //cleanup the field return false; }); </script> </body> </html> 

how a client publishes a specific Redis chat channel.

+9
Sep 18 '11 at 19:02
source share
2 answers

If you use the redis pub / sub function in your node.js program, you must select one redis client connection to listen on some channel and a second redis client connection to send regular commands and / or publish messages to your channel (s). From node_redis docs:

When the client issues SUBSCRIBE or PSUBSCRIBE, this connection is put into "pub / sub" mode. At this point, only commands that change the subscription set are valid. When the subscription set is empty, the connection returns to normal mode.

If you need to send regular commands to Redis in pub / sub mode, just open another connection.

Your problem is also related to these questions:

  • Redis / node.js - 2 clients (1 pub / sub) causing write problems
  • Why don't I have one Redis client running as PUB and Sub in the same connection?
+7
Sep 18 '11 at 19:26
source share

I believe that there is something missing in the example from this book, I also read this book and wondered. You are subscribed to a Redis feed and expect a server-side message, but you never publish that feed. What is missing is an event listener, so when there is a websocket message, you publish this message to the redis channel.

+1
Feb 21 '13 at 14:45
source share



All Articles