python-websockets / websockets

Library for building WebSocket servers and clients in Python
https://websockets.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
5.07k stars 506 forks source link

Supervisor and sub/sub together like fanout exchange? #1393

Closed IvesFeng closed 10 months ago

IvesFeng commented 10 months ago

I followed the tutorial(Integrate with Django) to write a websocket service, and then deployed it using Supervisor. I also configured the "numbrocs" to 4. Now I have a problem that all processes subscribe to redis messages, and the variable CONNECTIONS is different in each process. How do I send the message to the correct process? Or should I not use supervisor + pub/sub?

aaugustin commented 10 months ago

How do I send the message to the correct process?

There are several options here:

  1. Each process reads all messages, but ignores those for connections that it doesn't manage (i.e. connections not found in CONNECTIONS.
  2. You create as many Redis queues as you have processes (perhaps with a random identifier to keep things simple) and each process subscribe to their own queue. When a client connections, you store in a new Redis structure the information of which queue handles which clients with hset queues <client_id> <queue_id>. When sending a message, first you find out which queue it goes to with hget queues <client_id>, then you send the message to that queue.