Reputation: 1105
I was following the example this blog and the question arose me after implementing
https://github.com/DanialK/ReactJS-Realtime-Chat
Summarizing, before send a message via websocket the state of messages is updated. And when server receives that message, they send a broadcast to all clients, indluding myself. Thereat, client updates the state with this same message
Why this message does not appear 2 times? I don't want that message appear 2 times, but I want to know why it happens
Client code:
socket.on('send:message', this.messageRecieve);
...
handleMessageSubmit : function(message){
Messages.push(message);
this.setState({ messages : Messages });
socket.emit('send:message', message);
},
messageRecieve: function(message){
Messages.push(message);
this.setState({ messages : Messages });
},
Server code:
socket.on('send:message', function (data) {
socket.broadcast.emit('send:message', {
user: name,
text: data.text
});
});
Upvotes: 1
Views: 544
Reputation: 4065
The server does not send the message to the connected user.
As per the socket.io documentation, this API sends to all but the owner of the socket:
socket.broadcast.emit('send:message')
https://github.com/DanialK/ReactJS-Realtime-Chat/blob/master/routes/socket.js
Upvotes: 5