0

On flask socket-io, if there are 2 users connected and user1 sends a message to user2, how can I intercept the payload being sent to user2 (on the app context of user2, in order to modify it)? I do not want to modify the payload being sent by user1 (this would be trivial).

Logically, I guess there should be a way to intercept the event on the context of user2 just before the payload is being transmitted on the ws.

Thanks!

ptou
  • 323
  • 4
  • 12

2 Answers2

1

A monkey patching of custom_emit_internal did the trick. Hopefully, it will not break at the next update!

ptou
  • 323
  • 4
  • 12
0

I think you are asking for a client-side feature. In the server, the only context that exists when a message is sent is that of the sender. The exchange goes like this:

client                          | server
---------------------------------------------------------------------------
user1 sends event to server     |
payload includes msg for user2  |
                                |
                                | server decodes the msg payload in
                                | user1's context and sends msg to user2
                                |
user2 receives msg from server  |

So as you see, in the server, user2 is not part of this transaction, only user1 matters. The server-side context for user2 may not even reside in the same server process, now that Flask-SocketIO can drive a farm of servers behind a load balancer. Also, the recipient of the event may not be a single user, it may be a group of users in a room, or even all connected users.

I'm not sure I understand the purpose of your request, but if you want to elaborate I may be able to provide advice.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Hi Miguel - typical case: before sending any message to recipient users (for instance that joined a room), I want to check whether the recipients token is not expired. This would have to run in the base manager somewhere just after Kombu (on another server) has received the event and is ready to send it on the ws. Other case: I need to encrypt (on top of TLS) with a different key per recipient - it makes sense to only encrypt it before pushing it in a specific WS. Thanks for your help! – ptou Jan 26 '16 at 14:52
  • Wouldn't it be as easy for you to check the token of your recipient(s) before sending the message? Or even better, to disconnect any users when their tokens expire, separately from the sending of messages? – Miguel Grinberg Jan 26 '16 at 18:34
  • Hi Miguel, this was only a subset of things I will need. The best example is really: User1 sends payload to a room with User2&User3. User2 needs to receive (payload + specific payload for user2 only available in user2 context). Same for user3. Do you think it it possible to have a callback of the event manager just before publishing on a WS? Thanks for your help Miguel – ptou Jan 27 '16 at 00:21
  • What do you mean specifically by "user2 context"? Anything that is available in the user2 session the user2 in the client can request directly, why does it need to be appended to a message sent by another user? – Miguel Grinberg Jan 27 '16 at 01:01
  • for instance, let's imagine the server needs to append ws message seq specific for any message sent to the user2 - and that's the counter is in memory in the server where user2 is connected. – ptou Jan 27 '16 at 09:52