view raw
Flion Flion - 9 months ago 60
Node.js Question

not recieving events with this basic + redis setup?

I've got a simple server using, an, and I run redis (which is completely new to me, I'm on windows, I downloaded redis and opened redis-server.exe and redis-cli.exe).
Through the redis CLI with the command

I see that the server is connected and that events reach redis from the emitter, but
never logs anything. What else do I need to do? Does the server need to subscribe to redis?

Redis output
"publish" "" "\x93\xa7emitter\x83\xa4type\x02\xa4data\x92\xa4test\xa9some data\xa3nsp\xa1/\x82\xa5rooms\x90\xa5flags\x80"


var server = require('http').Server();
var io = require('')(server);
var redis = require('');

io.adapter(redis({ host: '', port: 6379 }));
io.on('connection', function(socket){
console.log('client connected'); // Works
socket.emit('connect','test'); // Works
io.on('test', function(socket){
console.log('test came in'); // Works


var io = require('')({
io.emit('test', 'some data');
}, 5000);


the answer is that was not what I needed for this test case. It connects directly to redis. In the example below I now use the usual and all events properly arrive in the server, and also with two servers and two clients connected each to a different server, with io.emit both clients now recieve the event. Great!


var PORT = 3000;
var HOST = 'http://localhost';
var port = parseInt(process.argv[2]) || PORT;
var io = require('');
var socket = io.connect(HOST + ':' + port);

socket.on('connect', function () {
    console.log('connected, sending message');
    socket.emit('message', 'message from client');
    socket.on('message', function(data) {
        console.log('new message received: '+data);


var port = parseInt(process.argv[2]) || 3000;
console.log('server listens on port ' + port);
var io = require('').listen(port);
var redis = require('');
var adapter = redis({ host: '', port: 6379 });
io.on('connection', function(socket){
    console.log('client connected');
    io.emit('message', 'client logged in on '+port+' and this message will be send to all clients');
    socket.on('message', function(d){
        console.log('message from socket:',d);

start 4 different terminals and run:

$ node test-server 3000
$ node test-server 3001
$ node test-emit 3000
$ node test-emit 3001