1

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
    });
});
3
  • I don't receive me own message?
    – patricK
    Sep 1, 2014 at 2:11
  • We don't know if you receive this message, look in your logs and tell us. We can't know if this is a React render problem or a Websocket/server problem... Sep 1, 2014 at 8:19
  • I asked this because I suspected that this would be a behavior of socket.io...
    – patricK
    Sep 1, 2014 at 20:48

1 Answer 1

5

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

1
  • I didn't found at socket.io docs it, however it is how it works, that explains my doubts, thank you
    – patricK
    Sep 1, 2014 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.