miguelgrinberg / flask-sock

Modern WebSocket support for Flask.
MIT License
274 stars 24 forks source link

Server-initiated broadcast to specific users #13

Open atarashansky opened 2 years ago

atarashansky commented 2 years ago

Is it possible to send a message to a specific user as opposed to responding to an event triggered by clients?

miguelgrinberg commented 2 years ago

It is, but the application has to handle it, this extension does not provide that as a feature. You have to store the ws objects for your clients so that you can send data on the appropriate one. For example, you can have a global dict with the user ID as key, and the ws object for that user as value. Then when you need to send a message to a specific user, you can look up the ws object in this dict.

Also, you may also consider using the Flask-SocketIO extension, which is a much more advanced solution that provides access to individual users or groups of them.