When calling `socket.to(roomId).emit`, the current socket won't receive this event even it has joined the room.
See original GitHub issueNote: for support questions, please use one of these channels: stackoverflow or slack
For bug reports and feature requests for the Swift client, please open an issue there.
For bug reports and feature requests for the Java client, please open an issue there.
You want to:
- report a bug
- request a feature
Current behaviour
What is actually happening?
I am compare the two apis:
socket.to(roomId).emit(...)
vs socket.broadcast.to(roomId).emit(...)
By now, even if the the sender socket is in the target room, both apis won’t send this event to it (or it doesn’t want to receive the event?).
Steps to reproduce (if the current behaviour is a bug)
Note: the best way (and by that we mean the only way) to get a quick answer is to provide a failing test case by forking the following fiddle.
const logger = console;
io.of("/test-room").on("connect", socket => {
const sid = socket.id;
logger.info("client connected", { sid });
socket.on("c.join-room", ({ roomId }, ack) => {
logger.info("client join room [", { sid, roomId });
socket.join(roomId, () => {
logger.info("client join room ]", { sid, roomId });
ack();
});
});
socket.on("c.leave-room", ({ roomId }, ack) => {
logger.info("client leave room [", { sid, roomId });
socket.leave(roomId, () => {
logger.info("client leave room ]", { sid, roomId });
ack();
});
});
socket.on("c.message", ({ roomId, message }) => {
logger.info("client sent message", { sid, roomId, message });
socket.to(roomId).emit("s.message", message);
// socket.broadcast.to(roomId).emit("s.message", message);
});
socket.on("disconnect", reason => {
logger.info("client disconnected", { sid, reason });
});
});
Expected behaviour
What is expected?
With socket.to(roomId).emit(...)
, the socket itself, if it joined the room, will receive this event. But with socket.broadcast.to(roomId).emit(...)
, it won’t receive the event.
Setup
- OS: mac 10.14.5
- browser: chrome 74.0.3729.169
- socket.io version: 2.2.0
Other information (e.g. stacktraces, related issues, suggestions how to fix)
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (1 by maintainers)
Top GitHub Comments
Same issue as @berksafran, emitting a message with
It’s not working but with :
It works like a charm
@ralphpig it’s not necessary include the namespace if your application design use default namespace “/” for everything, you can check this on emit cheatsheet
https://socket.io/docs/emit-cheatsheet/