3

I'd like to know if there's a way to communicate directly between two (or more) flask-socketio servers. I want to pass information between servers, and have clients connect a single web socket server, which would have all the combined logic and data from the other servers.

I found this example in JS Socket IO Server to Server where the solution was to use a socket.io-client to connect to another server.

I've looked through the Flask-SocketIO documentation, as well as other resources, however it doesn't appear that Flask-SocketIO has a client component to it.

Any suggestions or ideas?

Community
  • 1
  • 1
Phil
  • 33
  • 1
  • 4

1 Answers1

6

Flask-SocketIO 2.0 can (maybe) do what you want. This is explained in the Using Multiple Workers section of the documentation.

Basically, the servers are configured to connect to a shared message queue service (redis, for example), and then a load balancer in front of them assigns clients to any of the servers in the pool using sticky sessions. Broadcasting operations are coordinated automatically among the servers by passing messages on the queue.

As an additional feature, if you use this set up, you can have any process connect to the message queue to post messages for clients, so for example, you can emit events to clients from a worker or other auxiliary process that is not a SocketIO server.

From your question it is unclear if you were looking to implement something like this, or if you wanted to have the servers communicate for a different reason. Sending of custom messages on the queue is currently not supported, but your question gave me the idea, this might be useful for some scenarios.

As far as using a SocketIO client as in the question you referenced, that shouud also work. You can use this Python package: https://pypi.python.org/pypi/socketIO-client. If you go this route, you can have a server be a client and receive events or join rooms.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Thank you! This is very helpful. To be more specific about what I'm trying to achieve: I'm using multiple Raspberry Pi computers to integrate different sensors and control electronics. So while I can have a web-dashboard on a client device open multiple web sockets to connect to each Raspberry Pi server, what I'm looking for is to enable two Raspberry Pi servers to exchange data, and consolidate and/or act on multiple inputs in the back end before deciding what to do or present to a client. Think temp sensor server sending data to furnace control server to turn the heat up or down. – Phil Jan 15 '16 at 17:10
  • Basically you have two options. You can do what I mentioned in the last paragraph (each Pi is a SocketIO server, and also a SocketIO client to the other servers), or, probably more efficient, put a redis server in one of the nodes and have all the nodes connect to it and share messages via Pub/Sub. This latter approach is what I implemented to use internally in version 2.0 of the extension, but as I mention above, there is currently no support for custom use of the queue, you have to do that by talking directly to the queue. – Miguel Grinberg Jan 15 '16 at 20:52