You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello everyone, can anyone help me to figure out why the socket.emit() fires multiple times here is my code:
##Frontend
First Part: where i set message for current user and emit that message to the server
` const handleMessage = (e)=>{
setMessages([...messages,{time:new Date().getTime(),message:message,user:props.user && props.user.name}])
socket.emit("chat-message",{time:new Date().getTime(),message:message,user:props.user && props.user.name});
setMessage('');
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello everyone, can anyone help me to figure out why the socket.emit() fires multiple times here is my code:
##Frontend
First Part: where i set message for current user and emit that message to the server
` const handleMessage = (e)=>{
setMessages([...messages,{time:new Date().getTime(),message:message,user:props.user && props.user.name}])
socket.emit("chat-message",{time:new Date().getTime(),message:message,user:props.user && props.user.name});
setMessage('');
}
//Recieving message
socket.on("chat-message",(message)=>{
console.log(message);
setMessages([...messages,message.message])
})`
##Backend
`io.on("connection",(socket)=>{
socket.on("join-meet",(code)=>{
console.log(code.user)
console.log("Meeting joined by",socket.id);
console.log(code.code)
socket.join(code.code);
users.removeUser(socket.id);
users.addUser(socket.id,code.user.name,code.user.email,code.code,code.user.avatar)
io.to(code.code).emit("user-connect",users.getUserList(code.code));
socket.broadcast.to(code.code).emit("system-message",{message:users.getUser(socket.id).name+" joined the meeting"});
socket.on("chat-message",(message) => {
socket.to(code.code).emit("chat-message",{message:message});
})
socket.on("leave",(user)=>{
})
socket.on("disconnect",()=>{
console.log("User disconnected");
const user = users.removeUser(socket.id);
if(user){
io.to(user.room).emit("user-connect",users.getUserList(user.room));
io.to(user.room).emit("system-message",{message:user.name+" leave the meeting"});
}
})
})`
Beta Was this translation helpful? Give feedback.
All reactions