0

I am building a chat application using flask socketio and I want to send to a specific singular client and I'm wondering how to go about this.

I get that emit has broadcast and include_self arguments to send to all and avoid sending oneself, but how exactly would I go about maybe emitting to a single sid?

I've built this application using standard TCP/UDP socket connection where upon client connecting, there socket info was stored in a dictionary mapped to their user object with attributes that determined what would be sent and when I wanted to emit something to the clients I would iterate through this and be able to control what was being sent.

I'm hoping some mastermind could help me figure out how to do this in flask socket io

Bl3nder
  • 82
  • 5
  • Not much can be said here, you grab the socket from your dictionary, and do socket.emit(). Are you asking how to store the relationship between a socket, and your actual "user"? – i.brod Mar 28 '20 at 12:01

2 Answers2

1

To emit to a single user, you can use the user’s sid. For more info on that, visit this discussion.

If you have access to the client application, you could set up different rooms, let the clients join the different rooms according to your criteria, and have your server application emit to the different rooms. For more info on that you can have a look at the socket.io documentation about rooms and namespaces and the python-socketio documentation about rooms.

Elisabeth Strunk
  • 516
  • 1
  • 6
  • 15
1

I ended up figuring it out. Using the flask request module, you can obtain the users sid using request.sid, which can be stored and emitted to within the room parameter emit(..... room=usersid

Bl3nder
  • 82
  • 5