Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign up[QUESTION] Use of socket.io #129
Comments
|
Socket-IO has an ASGI compatible app: https://python-socketio.readthedocs.io/en/latest/server.html#uvicorn-daphne-and-other-asgi-servers You should be able to mount that app like if it was a FastAPI sub-application: https://fastapi.tiangolo.com/tutorial/sub-applications-proxy/#mount-the-sub-application |
|
Hi, Also have a question regarding python-socketio, Ive tried:
Which does indeed seem to work, but only partially. Only GET requests seems to get through
Is there a way to also allow POST on Thanks |
|
I am unable to get this to work at all, even with the snippet above. |
|
I see, here is my complete example,
Where the
I then run uvicorn with the It seems that the routing mechanism in either FastAPI ot Starlette somehow only allows GET requests, but I might be wrong and I'm maybe doing something else wrong. @BlackHoleFox: it did not work at all for me either until I noticed that second parameter of |
|
Finally,
Works just fine if you use the |
|
@marcus-oscarsson I ended up with the same results. I can directly use the |
|
@BlackHoleFox I see, I simply removed the |
|
So are we giving up on |
|
@kientt86 I would say that its up to the maintainer, @tiangolo, to decide exactly what to do with app.mount. I'm not sure if I used it correctly in my example so it could also simply be that I'm missing some option. It would be nice if it worked with Using the |
|
Thanks for all the reports guys. I want to check how to integrate Socket.IO and integrate it directly in the docs, I haven't had the time though, but I'll do it hopefully soon. |
|
I´m looking forward for this integrate. It would be great for the project. cheers! |
|
This is more of a feature request but related to using Hoping this can be implemented on top of the general |
|
I'm struggling with getting this to work with FastAPI. I've tried this code: fast_app = FastAPI(
openapi_url='/api/notifications/openapi.json',
docs_url='/api/notifications/docs',
redoc_url='/api/notifications/redoc'
)
sio = socketio.AsyncServer(
async_mode='asgi',
cors_allowed_origins='*'
)
app = socketio.ASGIApp(
socketio_server=sio,
other_asgi_app=fast_app,
socketio_path='/api/notifications/socket.io/'
)Which allows me to connect to the socket.io endpoint, but when I attempt to connect to a FastAPI defined endpoint, it'll raise a traceback from python-engineio:
As best I can tell, engineio is attempting to call FastAPI with 4 parameters ('self', 'scope', 'receive', and 'send') .. while 0.33 version of FastAPI is simply using the Starlette call method that only accepts ('self' and 'scope'). I'm interested to learn what version other people (@marcus-oscarsson ) are using where this works inside FastAPI app. I've managed to hack around it with this helper class: from fastapi import FastAPI
from starlette.types import ASGIInstance, Scope, Receive, Send
class FastAPISocketIO(FastAPI):
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> ASGIInstance:
fn = super(FastAPISocketIO, self).__call__(scope=scope)
return await fn(receive=receive, send=send)But it seems the problem is an incompatibility with Starlette and python-socketio? |
|
Turns out my problem was with Starlette. Starlette version 0.11.1 needed the above hack, while version 0.12.0 doesn't have the problem and works without the above hack. |
|
A bit more complete example, based on @szelenka's work that might help other people.
|
|
Any news? I look forward to it working. |
|
@mjmare Very good... that's works perfectly using uvicorn. But with gunicorn WSGI got some errors:
After some testing, I'm using this code: app = FastAPI(
title=config.PROJECT_NAME,
description=config.PROJECT_NAME,
version=config.PROJECT_VERSION,
debug=True
)
sio = socketio.AsyncServer(
async_mode='asgi',
cors_allowed_origins=','.join(config.ALLOW_ORIGIN)
)
sio_asgi_app = socketio.ASGIApp(
socketio_server=sio,
other_asgi_app=app
)
app.add_route("/socket.io/", route=sio_asgi_app, methods=['GET', 'POST'])
app.add_websocket_route("/socket.io/", sio_asgi_app)kind off working with gunicorn... sometimes, got the error above... I will keep trying and let you guys know... Cheers |
|
Can anyone provide some insight into whether they've gotten this to work well with a synchronous database connection (I'm using SQLAlchemy ORM) and what it takes to ensure thread-safety? |
I'm receiving the same error with running using Gunicorn. Running with Uvicorn doesn't get me the same response. |
|
I'm struggling with the same problem :( If I have 4 workers ( |
|
Has there been any update on this? Still struggling to find a complete working example. Thanks. |
|
There are a lot of different pieces in play here (gunicorn, uvicorn, starlette, fastapi, socket.io, python, ...), each of which is potentially relevant to figuring out if there is a problem. My strong suspicion is that this issue is not specific to FastAPI, but rather is a problem with uvicorn or starlette. It's probably worth trying to replicate the problem using just starlette and/or creating an issue in the starlette repo asking for guidance. At any rate, if you are having an issue and want help, please post the versions of each of the various dependencies you are using. That may not be enough to help resolve the issue, but as things stand it's basically impossible to help without knowing more. |
|
I had the same problem using the given example so I started from the python-socketio asgi example and added fastapi and it works I upgraded a few packages compared to the example and here is my
And then the code is the example #!/usr/bin/env python
import socketio
from fastapi import FastAPI
app = FastAPI()
sio = socketio.AsyncServer(async_mode='asgi')
socket_app = socketio.ASGIApp(sio, static_files={'/': 'app.html'})
background_task_started = False
async def background_task():
<... EXACTLY LIKE app.py IN python-socketio/examples/server/asgi ...>
@sio.on('disconnect')
def test_disconnect(sid):
print('Client disconnected')
@app.get("/hello")
async def root():
return {"message": "Hello World"}
app.mount('/', socket_app)Then used |


Description
How can I use socket.io instead of the plain websocket integration? I guess this is more a question belonging to starlette.
Currently migrating from a flask application using flask-socketio / python-socketio
Any hint is appreciated. Thx.