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:
client.emit("test", {
room: props.room,
});
client.on("testResponse", () => {
console.log("test response app");
});
server side :
socket.on("test", (data: { room: string }) => {
console.log("test");
io.in(data.room).emit("testResponse", {});
});
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
useEffect(() => {
socket.on("testResponse", () => {
console.log("test response app");
});
return () => {
socket.off("testResponse");
};
});