question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When calling `socket.to(roomId).emit`, the current socket won't receive this event even it has joined the room.

See original GitHub issue

Note: 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:closed
  • Created 4 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
CroquetMickaelcommented, Nov 25, 2020

Same issue as @berksafran, emitting a message with

 io.in("game").emit("big-announcement", "the game will start soon");

It’s not working but with :

socket.nsp.in(roomName).emit("big-announcement", "the game will start soon");

It works like a charm

1reaction
FifineHexcommented, Jun 14, 2019

@ralphpig it’s not necessary include the namespace if your application design use default namespace “/” for everything, you can check this on emit cheatsheet

// sending to all clients in 'game' room, including sender
io.in('game').emit('big-announcement', 'the game will start soon');

https://socket.io/docs/emit-cheatsheet/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does socket.io not join a given room? - node.js
Your client is actually joining the room , but it is not sending the message . As you are sending message from the...
Read more >
Rooms
A room is an arbitrary channel that sockets can join and leave. It can be used to broadcast events to a subset of...
Read more >
Chat Rooms With Socket.io
This event will simply emit a message to the client side that can be displayed as a notification. Finally, a users event is...
Read more >
How To Create a Real-Time App with Socket.IO, Angular, ...
When the client emits the getDoc event, the socket is going to take the payload (in our case, it's just an id), join...
Read more >
Real-time data transfer with Socket.io
What is WebSocket? WebSocket is a web communication protocol that allows for the transfer of data from the client side to the server...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found