socket.emit not sending to all clients
See original GitHub issueSocket.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:
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Probably what people are looking for would be here: https://socket.io/docs/v4/server-api/#server-sockets
Here we go! https://github.com/socketio/socket.io-website/commit/2d13db7ed3c83aa5c1d4fc1d5ca6afc1ef2b189e