pyropy / fastapi-socketio

Easily integrate socket.io with your FastAPI app 🚀
Apache License 2.0
328 stars 31 forks source link

`socketio_path` is incorrect in call to `socketio.ASGIApp` since FastAPI 0.109.0 / Stalette 0.33.0, breaking, well, everything #52

Open dhdaines opened 4 months ago

dhdaines commented 4 months ago

The default mounting point of /ws/socket.io no longer works correctly for quite some time now, because of this change to Starlette (which, apparently, is just following the ASGI spec): https://github.com/encode/starlette/discussions/2413#discussioncomment-8108358

First, you will have noticed that your JavaScript code using socket.io gives lots of 404 errors (or 403 errors, perhaps) and you will have tried to fix them by doing this:

var magicalMysteryOptions = { path: '/ws/socket.io', transports: ['websocket', 'polling', 'flashsocket']};
var conversionSocket = io(magicalMysteryOptions);

But, sadly, now you have another problem, which is a very large stacktrace on the server side with this in it:

RuntimeError: Expected ASGI message 'websocket.accept', 'websocket.close', or 'websocket.http.response.start' but got 'http.response.start'.

You have two choices, either you can downgrade to fastapi==0.108.0 (not recommended), or you can add a socketio_path argument to your SocketManager constructor:

socket_manager = SocketManager(
    app=app,
    socketio_path="/ws/socket.io",
)

This needs to be fixed in fastapi-socketio so that it passes the full path to socket.io (that is, prefixed with the mount location) in the ASGIApp constructor, and then fastapi-socketio needs to depend on at least the versions of FastAPI and Starlette mentioned above.

Since just joining mount_location and socketio_path will conflict with the workaround above, it would probably be a good idea to respect an absolute socketio_path if specified...

dhdaines commented 4 months ago

This is probably the same issue as #51 and #48 and maybe others