Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis pub/sub for chat server in node.js

I'm trying to work 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 am successful through here but never get "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 this

<!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 does a client publish to a specific Redis "chat" channel.

like image 585
Charles Short Avatar asked Sep 18 '11 19:09

Charles Short


1 Answers

If you are using redis pub/sub functionality within your node.js program you should dedicate one redis client connection for listening on some channel and second redis client connection for sending normal commands and/or publishing messages to your channel(s). From node_redis docs:

When a client issues a SUBSCRIBE or PSUBSCRIBE, that connection is put into "pub/sub" mode. At that point, only commands that modify the subscription set are valid. When the subscription set is empty, the connection is put back into regular mode.

If you need to send regular commands to Redis while 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 issues with writes
  • Why can't I have a single Redis client acting as PUB and Sub in the same connection?
like image 71
yojimbo87 Avatar answered Oct 05 '22 22:10

yojimbo87