FastAPI websocket can`t handle a large incoming of streams?
See original GitHub issueHi there. I have am using FastAPI websocket on Docker in my Ubuntu server. I have come very far in finishing it, and am currently in the test phase before everyting is “completed”.
While running different tests, I experienced a strange problem. From my client side, I am simulating an IoT devices sending realtime data including “position”, meaning as long as it is moving, it sends JSON object including its position. Im extremely confused at this point. Is it my code? Is my Dockerfile, is the linux server the problem? Therefore i feel the need to share all my file.
Before sharing my websocket code, here is my simple client code which i use to send data to my websocket server:
from websocket import create_connection
import json
import time
ws = create_connection("ws://139.59.210.113:5080/ws/testchannel")
time.sleep(1)
def send_json_all_the_time(position):
generate_json = { "machineID":"001", "RepSensor": position}
send_json = json.dumps(generate_json)
print("JSON SENT FROM IoT SENSOR: {}".format(send_json))
time.sleep(0.3)
ws.send(json.dumps(send_json))
time.sleep(0.3)
while True:
for x in range(100):
send_json_all_the_time(x)
for x in range(100, -1, -1):
ws.send("pause a little bit, starting again soon!")
send_json_all_the_time(x)
The code above is simply runned locally from my computer, and sends a lot og realtime data to the websocket server.
The code snippet below is taken from my websocket script that is runned on the linux ubuntu server:
@app.websocket("/ws/testchannel")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
try:
while True:
data = await websocket.receive_text()
print("Received data: {} ".format(data))
await websocket.send_text(f"you sent message: {data}")
await connection_manager.send_message_to_absolutely_everybody(data)
"""
PROBLEM: if i use this " await connection_manager.send_message_to_absolutely_everybody(data) " line, than i get delay into the server.
I think by optimizing the code this can be solved. You can test by commenting out the line 408 and you will see the difference.
"""
except WebSocketDisconnect:
print("client left chat.")
"""
The second problem: when a IoT device send a data to the server in real time and high speed, it gets disconnected randomly!
"""
So while I am watching the terminal, the data comes in… but then “client left the chat” occurs randomly!
So my question is, is the problem at the script? the server? dockerfile? docker commands?
For instance, i share also my Dockerfile as well. And if someone is interested, i could invite to my private github repo.
FROM ubuntu:latest
FROM python:3
MAINTAINER raxor2k "xxx.com"
RUN apt-get update -y
RUN apt-get install -y python3-pip build-essential python3-dev
COPY . /app
WORKDIR /app
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
RUN pip3 install fastapi uvicorn #dennekanfjernes?
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80", "--reload"]
Any answers or help would be appreciated!
Issue Analytics
- State:
- Created 3 years ago
- Comments:16 (1 by maintainers)
Top GitHub Comments
add this:
check this out for more detail about gunicorns configs