Reputation: 1011
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
Upvotes: 5
Views: 5031
Reputation: 986
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');
Upvotes: 2