I'm trying to connect a websocket aiohttp client to a fastapi websocket endpoint, but I can't send or recieve any data because it seems that the websocket gets closed immediately after connecting to the endpoint.
server
import uvicorn
from fastapi import FastAPI, WebSocket
app = FastAPI()
@app.websocket('/ws')
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
...
if __name__ == '__main__':
uvicorn.run('test:app', debug=True, reload=True)
client
import aiohttp
import asyncio
async def main():
s = aiohttp.ClientSession()
ws = await s.ws_connect('ws://localhost:8000/ws')
while True:
...
asyncio.run(main())
When I try to send data from the server to the client when a connection is made
server
@app.websocket('/ws')
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
await websocket.send_text('yo')
client
while True:
print(await ws.receive())
I always get printed in my client's console
WSMessage(type=<WSMsgType.CLOSED: 257>, data=None, extra=None)
While in the server's debug console it says
INFO: ('127.0.0.1', 59792) - "WebSocket /ws" [accepted]
INFO: connection open
INFO: connection closed
When I try to send data from the client to the server
server
@app.websocket('/ws')
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
while True:
await websocket.receive_text()
client
ws = await s.ws_connect('ws://localhost:8000/ws')
await ws.send_str('client!')
Nothing happens, I get no message printed out in the server's console, just the debug message saying the client got accepted, connection opened and closed again.
I have no idea what I'm doing wrong, I followed this tutorial in the fastAPI docs for a websocket and the example there with the js websocket works completely fine.