Use socket.broadcast
/ io.emit
to send data to all connections
In your server-side code …
socket.on('new-ops', data => {
const data2 = JSON.parse(data);
socket.emit('new-remote-ops', JSON.stringify({
name: data2.name,
age: data2.age + 10
}), (err) => console.log(err));
})
… you receive and send data to the same socket
. This will always be the same socket
.
If you want to receive data from individual sockets but repond to all available connections you have to use broadcast
(here) or io.emit
(here)
Example
// everyone gets it but the sender
socket.broadcast.emit("an event", { some: "data" });
// everyone gets it
io.emit("an event sent to all connected clients"); // main namespace
Socket.io Version 4.x
CLICK HERE to find out more related problems solutions.