django-websocket-redis icon indicating copy to clipboard operation
django-websocket-redis copied to clipboard

Excluding message sender

Open Glideh opened this issue 11 years ago • 12 comments

Is there a way to exclude the sender from the message broadcast subscribed to the same context ? Would it be interesting to have this option in the audiences ?

Glideh avatar Apr 25 '14 10:04 Glideh

I did some work on this some time ago. But I'm still unsure, if it does not have unwanted side-effect, when mixing different kinds of publishers/subscribers. Please check this branch https://github.com/jrief/django-websocket-redis/tree/exclude-message-sender and tell me, if it fits for you. I would btw. be interested which publishers/subscribers you're using.

jrief avatar Apr 25 '14 10:04 jrief

Interesting, but indeed sometimes you would need to be self notified. For a more global behavior I would maybe have given the ability to specify audiences like groups + not_users or groups + not_session which would allow to include or exclude contexts.

Glideh avatar Apr 25 '14 10:04 Glideh

Wouldn't that make the API far too complicate?

How about something simpler, say that the same message is never send back on its counterpart. Say ws4redis receives a message on publish-user, than that message would be sent back on subscribe-session, subscribe-broadcast and subscribe-group (of course, only if desired), but it would not be sent back on subscribe-user.

Please think about it. Something I would like to avoid, is to prefix each message with a unique ID. We certainly need a simple but generic solution for this.

jrief avatar Apr 25 '14 11:04 jrief

I agree with a simple & generic solution. I'm wondering if user & group are not already too specific. Maybe we should simply try to get closer to the other implementations (I don't know them though)

Glideh avatar Apr 25 '14 12:04 Glideh

Maybe we should simply try to get closer to the other implementations (I don't know them though)

Me neither. Anybody here? Please give some hints.

jrief avatar Apr 25 '14 12:04 jrief

I just found this example where socket.io uses to() & in() to respectively include or exclude the sender.

Glideh avatar Apr 25 '14 12:04 Glideh

Hmm, Interesting idea, but I have two problems with it.

How can the client programmatically tell the server how and in what he's interested? With Socket.IO, the client could pass in this execution stack using JS commands, which then are evaluated on the server using eval(). This however is very dangerous and evil.

The second problem is, that the server never talks to itself. It publishes to Redis and Redis triggers a subscription event. Therefore the

 socket.on('cursor', function(data) {
  socket.broadcast.emit('msg', data);
});

callback doesn't work here.

... but I'm sure, we'll come up with a feasible idea.

jrief avatar Apr 25 '14 13:04 jrief

Yeah I was also wondering about the same type of question

Glideh avatar Apr 25 '14 14:04 Glideh

I was thinking about how to solve this problem: Each message is prefixed with a number consisting of the current threat/greenlet-ID and a message counter, which is increased after publishing a message. This way, duplicate messages can be avoided.

However, if you try to run the chat-demo using the branch exclude-message-sender, it won't work correctly anymore.

I'am still struggling...

jrief avatar Apr 26 '14 22:04 jrief

@Glideh I just commited a new version on branch exclude-message-sender. I hope this now is a good solution:

There is a message class which wraps messages into a list with versioning information. The first ID ist the FD of the client's websocket. The second ID is a unique value the client can choose himself. For this ID I suggest to use new Date().valueOf(), since it is monotonically increasing and thus well suitable for comparisons against the past. The third item is the payload.

Please check the demo samples and tell me, if they fit your needs.

jrief avatar Apr 28 '14 14:04 jrief

I just checked your commits on exclude-message-sender. I'm not enough familiar with Python & websockets to understand the details but I get the idea. I don't know how to test it though. Also I don't really understand how can you know from the server which is the client_id to exclude.

Glideh avatar Apr 29 '14 08:04 Glideh

I just dropped theses changes. They don't work as expected. Currently the only new feature is that one can choose, if he wants to echo his own message or not.

jrief avatar Apr 29 '14 08:04 jrief