Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

727
Views
Can't get data from client in FastAPI websocket | Python

I get this error in my fastapi log:

INFO:     ('172.28.0.1', 35088) - "WebSocket /ws" [accepted]
INFO:     connection open
ERROR:    Exception in ASGI application
...
    await self.app(scope, receive, send)
    File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 58, in __call__
    await self.app(scope, receive, send)
    File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 580, in __call__
    await route.handle(scope, receive, send)
    File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 297, in handle
    await self.app(scope, receive, send)
    File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 68, in app
    await func(session)
    File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 273, in app
    await dependant.call(**values)
    File "/code/./app/main.py", line 127, in websocket_endpoint
    data = await websocket.receive_text()
     File "/usr/local/lib/python3.9/site-packages/starlette/websockets.py", line 85, in receive_text
     self._raise_on_disconnect(message)
    File "/usr/local/lib/python3.9/site-packages/starlette/websockets.py", line 80, in _raise_on_disconnect
    raise WebSocketDisconnect(message["code"])
    starlette.websockets.WebSocketDisconnect: 1000
    INFO:     connection closed

I can't find the problem. Why my fastapi route can't get the data from client but the server message to client works.

Client Code:

import asyncio
from time import sleep
import websockets

async def get_work():
    async with websockets.connect("ws://localhost:3000/ws", open_timeout=None) as websocket:
        await websocket.send('Hello from CLient')
        answer = await websocket.recv()
        print(answer)
         
while True:
    asyncio.run(get_work())
    sleep(5)

Server Code:

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        try:
            data = await websocket.receive_text()
            print(data)
        except RuntimeError:
            break
        await websocket.send_text("Hello from Server") 

Thanks a lot for help!

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!