I have a system that has a fastAPI server, a python client implemented on Raspberry and Javascript clients for the user interface. Data is sent from python client to server then forwarded to Js client and vice versa. I established the connection between the server and each type of client but when sending from a client-side to the server, it just send back to that client and the rest ones receive nothing. What is the proper way to deal with this problem? Hope for your help. Thanks.
Asked
Active
Viewed 326 times
2
-
please, add the server code that is responsible to establish connection to your clients – kosciej16 Dec 20 '21 at 00:57
-
1Are you saying you want the server to broadcast every input to all clients? You can do that, but that's not how TCP servers normally operate. If that's what you want, say so, and we can make some suggestions. – Tim Roberts Dec 20 '21 at 01:01
-
Yes, but not totally. I want to broadcast messages from a client to all remaining clients. I've done that thing with the socketIO library on NodeJS but in fastAPI server, I can't find how to do it. – Thanh Nhon Dec 20 '21 at 01:11
-
@kosciej16 J just took an example of WebSocket on FastAPI doc. In the first place, I want to do a basic task like receiving and sending text messages but it didn't work as expected. Just a sending client received message, not all clients. – Thanh Nhon Dec 20 '21 at 01:16
-
1I added an answer but I am aware it's quite complicated and need some of your research. Let me know if you stuck with something so will try to add more hints. – kosciej16 Dec 20 '21 at 01:52
1 Answers
1
The problem with websocket is it doesn't support broadcasting. You can store somewhere list of connected clients and iterate over them to send a message
CLIENTS = set()
async def broadcast():
while True:
await asyncio.gather(
*[ws.send("woof") for ws in CLIENTS],
return_exceptions=False,
)
await asyncio.sleep(2)
asyncio.create_task(broadcast())
async def handler(websocket, path):
CLIENTS.add(websocket)
try:
async for msg in websocket:
pass
finally:
CLIENTS.remove(websocket)
start_server = websockets.serve(handler, ...)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
You can read more information here
However my recommendation is to use socket.io. With it you can create rooms and send data to all clients that entered room with single emit.

kosciej16
- 6,294
- 1
- 18
- 29
-
Thanks for your help. I don't specialize in python very much so in your recommendation, I found python socket-io can do exactly what need. I made some research on it but still can't integrate it to fastAPI server. Could you give me an example of how to do that? That'll be appreciated. Thanks – Thanh Nhon Dec 20 '21 at 14:45
-
-
1@ThanhNhon Here is the snippet, let me know if it works for you and is there something I could clarify more https://stackoverflow.com/questions/70429135/how-to-mount-socket-io-to-fastapi-app-and-send-broadcast-to-all-connected-client/70429136#70429136 – kosciej16 Dec 20 '21 at 23:36
-
I'm so grateful for your help. It worked smoothly just by simple lines of code. Note about version for people who also search for this: I'm using python-socket io v5.5.0 and socket.IO v4 on Javascript client-side. In order to emit events from python-socketio client, you may also need to install "python-socketio[client]" – Thanh Nhon Dec 21 '21 at 10:21