Socket.io: Sending to all in room, including self

Created on 5 Mar 2013  路  8Comments  路  Source: socketio/socket.io

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?

Most helpful comment

FWIW in v1.0 you can do

socket.nsp.to(room).emit(event);

All 8 comments

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.

Was this page helpful?
0 / 5 - 0 ratings