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:
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...
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-8108358First, 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:But, sadly, now you have another problem, which is a very large stacktrace on the server side with this in it:
You have two choices, either you can downgrade to
fastapi==0.108.0
(not recommended), or you can add asocketio_path
argument to yourSocketManager
constructor:This needs to be fixed in
fastapi-socketio
so that it passes the full path tosocket.io
(that is, prefixed with the mount location) in theASGIApp
constructor, and thenfastapi-socketio
needs to depend on at least the versions of FastAPI and Starlette mentioned above.Since just joining
mount_location
andsocketio_path
will conflict with the workaround above, it would probably be a good idea to respect an absolutesocketio_path
if specified...