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.

socket.emit not sending to all clients

See original GitHub issue

Socket.io seems to be getting confused with its socket connections, preventing me from using socket.emit to send a message to all connected clients. Instead, it sends to just one out of the two (I only allow two connections).

const app = require('express')()
const http = require('http').createServer(app)
const constants = require('./constants')
const draft = require('./game/draft')
const io = require('socket.io')(http, {
    cors: {
        origin: "http://localhost:3000  ",
        methods: ["GET", "POST"]
      }
})

// auth middleware
io.use((socket, next) => {
    const username = socket.handshake.auth.username;
    if (!username) {
        return next(new Error("invalid username"));
    }
    socket.username = username;
    next();
});

io.on('connection', function (socket) {
    if (io.engine.clientsCount > constants.CONNECTION_LIMIT) {
        socket.emit('err', { message: 'reach the limit of connections' })
        socket.disconnect()
        console.log('Disconnected...')
        return
    }

    socket.broadcast.emit("user connected", {
        userID: socket.id,
        username: socket.username,
    });
    const users = [];
    for (let [id, socket] of io.of("/").sockets) {
      users.push({
        userID: id,
        username: socket.username,
      });
    }
    socket.emit("users", users);

     if (users.length === 2) {
         socket.emit('START_GAME', { message: 'both players have joined the lobby' })
    }
})

The expected behavior is for the above START_GAME message to go to both connected clients. What happens is that it only goes to one. I can actually send it to the other if I combine socket.emit with socket.broadcast.emit.

Seems like a genuine bug. Picture below:

pic of problem

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rfox12commented, Sep 12, 2021

Probably what people are looking for would be here: https://socket.io/docs/v4/server-api/#server-sockets

io.sockets.emit("hi", "everyone");
// is equivalent to
io.of("/").emit("hi", "everyone");
Read more comments on GitHub >

github_iconTop Results From Across the Web

SocketIO emit to all clients is not working - Stack Overflow
To emit a message to all connected clients, you can use io.sockets.emit (on the server): io.on('connection', (socket: Socket) ...
Read more >
Client API - Socket.IO
Emits an event to the socket identified by the string name. Any other parameters can be included. All serializable datastructures are supported, ...
Read more >
socket.io emit not working · Issue #3840 - GitHub
I am using socket.io client and server(nodejs) on connect I am using emit but it is not doing anything my server code const...
Read more >
io.emit() not emitting data to all clients : r/node - Reddit
the issue is sometimes it shows only some sockets and sometimes it shows the empty object. Some can help me if you have...
Read more >
Socket.IO - Broadcasting - Tutorialspoint
Broadcasting means sending a message to all connected clients. Broadcasting can be done at multiple levels. We can send the message to all...
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