socketio / socket.io-redis-emitter

The Socket.IO Redis emitter, allowing to communicate with a group of Socket.IO servers from another Node.js process.
https://socket.io/docs/v4/redis-adapter/
MIT License
722 stars 121 forks source link

How do I listen for events? Nothing is responding to emitter(s) #16

Closed ericterpstra closed 10 years ago

ericterpstra commented 10 years ago

I'm setting up an example app, and cannot figure out how to listen for events from socket.io-emitter (or socket.io-php-emitter). Everything seems to be working fine. I have two browsers that connect sucessfully to Socket.IO. The 'connection' event fires, and I can emit from Socket.IO back to the clients.

However, when I try to emit from socket.io-emitter (using the sample code in a separate process), or emit from PHP, I can see the publish events happening in Redis, but nothing happens on the connected clients. Is there anything special I need to do to pass events from socket.io-emitter to clients connected to Socket.IO?

My code is here:

// ===> socketapp.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
});

server.listen(3000);

// ===> Client/Browser JavaScript <===

$(function(){
    var socket = io('http://localhost:3000');

    socket.on('connect',function(){
        console.log("Client Connected!"); // Works
    });

    // Listen for events from socket.io-php-emitter
    socket.on('testPHPevent', function(data){
       console.log("Testing PHP Emitter -- " + data); // Nothing
    });

    // Listen for events from socket.io-emitter (running in a separate process)
    socket.on('time', function(data){
        console.log("Time " + data);  // Nothing
    })
});

// ===> socket.io-php-emitter code <===

        $redis = new \Redis();
        $redis->connect('127.0.0.1', '6379');
        $emitter = new SocketIO\Emitter($redis);

        $data = array(
            'thing' => 'test'
        );

        $emitter->emit('testPHPevent', $data);

// * REDIS SUBSCRIBE output (for socket.io-php-emitter)*

127.0.0.1:6379> SUBSCRIBE socket.io#emitter
...
1) "message"
2) "socket.io#emitter"
3) "\x92\x83\xa4type\x02\xa4data\x92\xactestPHPevent\x81\xa5thing\xa4test\xa3nsp\xa1/\x82\xa5rooms\x90\xa5flags\x90"

// ===> socket.io-emitter test application code <===

var io = require('socket.io-emitter')({
    host:'localhost',
    port:'6379'
});
setInterval(function(){
    io.emit('time', new Date);
}, 5000);

// * REDIS SUBSCRIBE output (for socket.io-emitter) *

1) "message"
2) "socket.io#emitter"
3) "\x92\x83\xa4type\x02\xa4data\x92\xa4time\xb82014-09-01T18:23:22.612Z\xa3nsp\xa1/\x82\xa5rooms\x90\xa5flags\x80"

Hopefully this is clear. Everything I'm doing here is based of sample code from README files and the Socket.IO website. It just looks like everything is getting "stuck" in Redis.

Also, I tried putting a console.log in the socket.io-redis adapter onmessage function, and it never happened. I'm not sure if this is a problem with socket.io-redis, or socket.io-emitter, or my code.

Any assistance would be appreciated. Thanks.

ericterpstra commented 10 years ago

Ok, my mistake. My app/emitters were running in a VM, and I accidentally started my main node process on my local machine. Need to pay closer attention to which terminal I'm typing my commands into. Everything is working as expected. Sorry :P

obipascal commented 2 years ago

Please i need help I'm facing the same error its upto two days now but couldn't figure out what am missing here any clear examples?