5

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
Kuryaki
  • 981
  • 2
  • 8
  • 18

1 Answers1

2

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');
Jujuleder
  • 986
  • 7
  • 12
  • From the docs socket.io-emitter allows you to communicate with socket.io servers easily from non-socket.io processes., im using it to fire messages to the clients from non-socket.io processes like non-socket.io exposed workers – Kuryaki Jun 04 '14 at 12:20
  • i edited my answer, you have only emit function, you have to handle the broadcast in server – Jujuleder Jun 04 '14 at 15:41
  • @Jujuleder Hi, could you take a look at my post ? i really cant seem to get socket.io-emitter, it's supposed to emit to all servers connected to the same redis pub and sub right ? but i cant log out the errors .. nor do i receive any events from my server https://stackoverflow.com/questions/48146965/i-am-unclear-of-how-to-use-socket-io-emitter-server-to-server – DarkArtistry Jan 08 '18 at 09:47