I am using Socket.io with react to do a chat room, when a new user join the room i want to send an update to all clients.
const updateDiscussionClients = room => {
let currentClients = RoomClients({room, io, connections})
console.log(currentClients); // printing correct data
console.log(io.sockets.adapter.rooms.get(room));
socket.to(room).emit('clients_discussion', currentClients)
}
socket.on("join_discussion", (data) => {
let room = 'room_' + data.id + data.name
socket.join(room)
updateDiscussionClients(room)
})
everything is working fine but the message received by old client, without the last one who
output of the logs:
[
{ name: 'Mohamad Zbib', id: '202100006' },
{ name: 'pu10', id: '10' }
]
Set(2) { 'pbKuaEdrfg7n83YyAAAD', '3QYOJCzlxxMOLquzAAAB' }
the event is received by pbKuaEdrfg7n83YyAAAD, but 3QYOJCzlxxMOLquzAAAB didn't receive it , he will receive if an update happened later
is 3QYOJCzlxxMOLquzAAAB sender of that event, which will not receive?
socket.to("room1").emit(/* ... */); will send to all in room except sender.
io.in("room1").emit(/* ... */); will send to all in room
Check this cheat sheet for more info