β

Socket.io with redis store emits events in the cal

User zangw - Stack Overflow 206 阅读

I want to use nodejs server to receive messages from AMQP, then push those messages to web page through socket.io with redis.

The emit() function is invoked in the callback function consume of AMQP, and web UI can get those data. However, I can NOT find any data in redis?

codes

var redisPort = 6379,
    redisHost = 'localhost';
var app = require('express')(),
    http = require('http').Server(app),
    io = require('socket.io')(http),
    redisAdapter = require('socket.io-redis'),
    redis = require('redis');
var
    pub = redis.createClient(redisPort, redisHost),
    sub = redis.createClient(redisPort, redisHost, {detect_buffers: true});
//
io.adapter( redisAdapter({pubClient: pub, subClient: sub}) );
app.get('/', function(req, res){
    res.sendFile('index.html');
});
io.on('connection', function(socket){
    console.log("web socket connection ...");
    socket.join('room');
    socket.on('disconnect', function(){
        console.log("socketio diconnect...");
    });
});

call back function in AMQP

function response(msg){
    io.to('room').emit('online', msg);
}
...
ch.consume(queue, response, {noAck: true});

Am I right to use this API emit()? or miss any good trick?

作者:User zangw - Stack Overflow
most recent 30 from stackoverflow.com
原文地址:Socket.io with redis store emits events in the cal, 感谢原作者分享。

发表评论