django-realtime-tutorial icon indicating copy to clipboard operation
django-realtime-tutorial copied to clipboard

Displaying duplicate messages

Open geojacobm6 opened this issue 9 years ago • 5 comments

While using socket.broadcast.emit('message', message); instead of socket.send(message); it displays multiple messages.

Actually i need to use socket.broadcast.emit() functionality.

geojacobm6 avatar Sep 05 '14 07:09 geojacobm6

Broadcast emit shouldn't be sending the event to the user who created it http://socket.io/docs/#broadcasting-messages

What version of socket.io and gevent-socket-io are you using?

mburst avatar Sep 05 '14 17:09 mburst

socket.io 0.9.16

geojacobm6 avatar Sep 06 '14 08:09 geojacobm6

Ok I think I understand the issue you're having. The message is coming from the redis pubsub channel in the case of this tutorial rather than a client side socket message. Since socket.io doesn't have a socket client id to filter out it for broadcast emit it just sends it to everyone.

You can test that this is the case by creating a client side event that sends to the server and then when the server receives that message have it broadcast emit an event. Then on the client side listen for that event. It shouldn't come up on the client that sent the message.

Let me know how that works out for you.

mburst avatar Sep 07 '14 19:09 mburst

Yes now it is working, but is it possible to broadcast to specified sockets from redis pubsub channel?

geojacobm6 avatar Sep 09 '14 05:09 geojacobm6

You definitely can. As you can see in this example https://github.com/Automattic/socket.io/blob/master/examples/chat/index.js the backend is storing a list of usernames. You could easily store a list of sockets and then iterate over them calling the socket.send method. You could also access the socket.io internal reference for connected sockets and do the same thing https://github.com/Automattic/socket.io/blob/master/lib/client.js#L31.

mburst avatar Sep 09 '14 06:09 mburst