I cannot seem to find a way how to emit event to all clients inside a room including self.
Basicly socket.in('myroom').broadcast.emit()
emits to all, except self and socket.in('myroom').emit()
sends only to self.
I know I can do both at the same time, but that doesn't feel effective to me.
How do I emit to all clients in the room, including self with one call?
Update:
Okay, I manged to emit to all with io.sockets.in('myroom').emit();
, but that made me introduce io
into global scope, that kind of ruins my decoupling.
Is there a way to do it through socket
object?
I have same problem with this. When I trying
socket.broadcast.to('myroom').emit('blaaaaa'); it doesnt work
sending only "debug - broadcasting packet"
try socket.manager.sockets.in('myroom').emit()
pass a callback to do the emit if you dont want to add io to scope
function sendRoom(room){
return function(data){ io.sockets.in(room).emit(data);}
}
doCheck(sendRoom("room"))
function doCheck(theRoom){
theRoom({some:"data"});
}
FWIW in v1.0 you can do
socket.nsp.to(room).emit(event);
@yads your example works well :+1: socket.io v 1.3.7
@yads worked for me as well, thank you
@yads good,
@yads your example can works, thanks you.
Most helpful comment
FWIW in v1.0 you can do
socket.nsp.to(room).emit(event);