Not getting events with this basic installation socket.io + redis?

I have a simple socket.io server using socket.io-redis, socket.io-emitter, 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, using the monitor command, I see that the server is connected and that events reach redis from the emitter, but test-server.js never writes anything. What else do I need to do? Is a socket.io server required to subscribe to redis?

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

test server.js

 var server = require('http').Server(); var io = require('socket.io')(server); var redis = require('socket.io-redis'); io.adapter(redis({ host: '127.0.0.1', 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 }); server.listen(3000); 

test emit.js

 var io = require('socket.io-emitter')({ host:'localhost', port:'6379' }); setInterval(function(){ io.emit('test', 'some data'); console.log('emitted'); }, 5000); 
+7
source share
1 answer

The answer is that socket.io-emitter not what I needed for this test case. It connects directly to redis. In the example below, I now use the usual socket.io-client , and all events are correctly sent to the server, as well as with two servers and two clients connected to each other server, with io.emit both clients receiving the event. Fine!

test emit.js

 var PORT = 3000; var HOST = 'http://localhost'; var port = parseInt(process.argv[2]) || PORT; var io = require('socket.io-client'); 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); }); }); 

test server.js

 var port = parseInt(process.argv[2]) || 3000; console.log('server listens on port ' + port); var io = require('socket.io').listen(port); var redis = require('socket.io-redis'); var adapter = redis({ host: '127.0.0.1', port: 6379 }); io.adapter(adapter); 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); }); }); 

run 4 different terminals and run:

 $ node test-server 3000 $ node test-server 3001 $ node test-emit 3000 $ node test-emit 3001 
+2
source share

All Articles