When emit a event to the websocket server, the server receive the event once and send a response, but the client receive the event multiple time: result expected result received client side:
JavaScript
x
7
1
client.emit("test", {
2
room: props.room,
3
});
4
client.on("testResponse", () => {
5
console.log("test response app");
6
});
7
server side :
JavaScript
1
5
1
socket.on("test", (data: { room: string }) => {
2
console.log("test");
3
io.in(data.room).emit("testResponse", {});
4
});
5
Advertisement
Answer
UPDATE: on the client side I got to put the socket.on(‘response,()=>{…}) in the UseEffect of the component and add a socket.off in the return of the useEffect
JavaScript
1
12
12
1
useEffect(() => {
2
socket.on("testResponse", () => {
3
console.log("test response app");
4
});
5
6
7
return () => {
8
socket.off("testResponse");
9
};
10
11
});
12