whythawk / full-stack-fastapi-postgresql

Full stack, modern web application generator. Using FastAPI, PostgreSQL as database, Nuxt3, Docker, automatic HTTPS and more.
MIT License
231 stars 47 forks source link

Websocket Support #28

Closed qodzero closed 11 months ago

qodzero commented 1 year ago

Hi, Im using this template for my project but I can't get websockets to work at all.

I simply added a quick websocket test to backend/app/main.py:

@app.websocket("/ws/{some_id}")
async def ws_test(websocket: WebSocket):
    await websocket.accept()
    print("Accepted")

When I visit http://localhost, I can see the default blog as expected, however when I connect to ws://localhost/ws/some-id using a websocket client, nothing happens, nothing printed on the server console either. The server just spits out:

"GET /ws/some-id HTTP/1.1" 200 14635 "-" "-" 171 "myapp-com-frontend-http@docker"

Could you help me on how to add websocket support here?

turukawa commented 1 year ago

Hi @qodzero ... the base generator doesn't have websockets built in, although I have used them in projects. You need both a FastAPI endpoint, as well as the Nuxt3 integrations. The backend basically acts as a listener in a perpetual while do loop for as long as the socket is open. But that's all controlled from the frontend.

I'll try and put a help guide together, but that might not be immediate if you're on a deadline?

qodzero commented 1 year ago

I'm not in much of a deadline, it's a personal project and I've managed to come up with a fix for the websocket integration. This can be closed now

turukawa commented 11 months ago

Hey @qodzero ... got your Websockets guide. Relatively simplified, but shows you how I run websockets with FastAPI and Nuxt.