Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to broadcast from socket.io-emitter

Im currently emitting messages using socket.io-emitter to emit messages (in namespace) from a worker in my app, however now i need to broadcast to all connected sockets(to the namespsace), when something happends, is there any work around there?

For example this is a socket.io exposed(HTTP) emit and broadcast using socket.io adapter to be able to run different socket.io instances in different processes

var io = require('socket.io')(http);
io.adapter(redis(config.redis));

io.of('/namespace').on('connection', function(socket){
    socket.emit('message', 'Hi you!');
    socket.broadcast.emit('broadcast', 'Heya all!');
});

This is now a different process (MQ worker) that is emitting events to the clients

var io = require('socket.io-emitter')(redis(config.redis));

var socket = io.of('/namespace');

socket.emit('message', 'Hi you!');            // This works
socket.broadcast('broadcast', 'Heya all!');   // This won't work
like image 507
Kuryaki Avatar asked Oct 31 '22 22:10

Kuryaki


1 Answers

It doesn't work this way.

With client-emitter you can only emit, then the server process what he want to do with this event.

Server-side :

socket.on('msg', function (msg) {

    socket.broadcast.emit('msg', msg);

});

client-side :

socket.emit('msg', 'msg');
like image 97
Jujuleder Avatar answered Nov 12 '22 17:11

Jujuleder