You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Everything seem to work well, so I proceeded to use the rewrites functionality of Next.js, which allows to use Next.js as frontend while using any other framework for backend in the same host, and rewrote all the traffic that would come to /api/* to http://localhost:8000/api/* which would be where my FastAPI and Socket.IO would listen for requests.
Once this configuration has been applied I should be able to make a request to http://localhost:3000/api and receive the response from my FastAPI app, and indeed, that's what happens, I receive the right response. The problem comes when I try to test the /api/socket.io endpoint by using a modified version of the socket_client code I showed previously.
This code won't work, it returns the following error:
socketio.exceptions.ConnectionError: Unexpected status code 404 in server response
Checking the uvicorn logs I see that the connection request was made but the endpoint couldn't be found.
[0] INFO: 127.0.0.1:62536 - "GET /api/socket.io?transport=polling&EIO=4&t=1720303873.2073076 HTTP/1.1" 404 Not Found
I thought that Next.js broke the Socket.IO app in some mysterious way, so I tried testing the app using it's original port (8000), which to my surprise worked.
[0] INFO: 127.0.0.1:62564 - "GET /api/socket.io/?transport=polling&EIO=4&t=1720304056.1841798 HTTP/1.1" 200 OK
I believed that I had made something wrong, so I completely rewrote my entire app but got the same result, then I checked the requests and noticed that there's a small difference: The lack of the trailing slash at the bad request, this was generating the 404 not found error as /socket.io/ is a different route regarding /socket.io, so I tried the most straightforward idea, to force the Next.js rewrites to add the trailing slash but it seems like it's not possible, another thing I thought that could work would be to rewrite using the trailing slash at the end of my destination such as:
But sadly this results in the rewrite not working and being more like a redirect to https://localhost:8000/api/*/. I know this is a Next.js problem and I'll open a issue there to enable the trailing slash in the rewriting, but I needed my software to work as soon as possible so I continued looking into this library and found that: It is not possible to create an endpoint without the trailing slash as this code found at async_drivers/asgi.py and middleware.py don't allow it.
I checked the official Socket.IO's documentation and found that adding the trailing slash when creating a server is an option that can be disabled, so I think it'd be good to have that option here too.
Describe the solution you'd like
I think that a good solution would be to create an optional add_trailing_slash parameter at the ASGIApp and WSGIApp init definitions, which default value can be True but can be changed to False in use cases like mine, I think that something like this would work:
I have already applied this solution on both, python-engineio and python-socketio in my forks and they seem to work well, I'll open the respective pull requests for this to be checked for the maintainer.
Describe alternatives you've considered
I considered rewriting my backend in JavaScript as the option exists in Socket.IO but that would take more time than just modifying this library a bit.
Additional context
This changes should also be applied to python-socketio for this option to work correctly.
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
I created a Socket.IO ASGI app and added a FastAPI app to it such as detailed here.
Posteriorly I ran this server using uvicorn and tested the Socket.IO app using the AsyncClient such as detailed here.
Everything seem to work well, so I proceeded to use the rewrites functionality of Next.js, which allows to use Next.js as frontend while using any other framework for backend in the same host, and rewrote all the traffic that would come to
/api/*
tohttp://localhost:8000/api/*
which would be where my FastAPI and Socket.IO would listen for requests.Once this configuration has been applied I should be able to make a request to
http://localhost:3000/api
and receive the response from my FastAPI app, and indeed, that's what happens, I receive the right response. The problem comes when I try to test the /api/socket.io endpoint by using a modified version of the socket_client code I showed previously.This code won't work, it returns the following error:
socketio.exceptions.ConnectionError: Unexpected status code 404 in server response
Checking the uvicorn logs I see that the connection request was made but the endpoint couldn't be found.
[0] INFO: 127.0.0.1:62536 - "GET /api/socket.io?transport=polling&EIO=4&t=1720303873.2073076 HTTP/1.1" 404 Not Found
I thought that Next.js broke the Socket.IO app in some mysterious way, so I tried testing the app using it's original port (8000), which to my surprise worked.
[0] INFO: 127.0.0.1:62564 - "GET /api/socket.io/?transport=polling&EIO=4&t=1720304056.1841798 HTTP/1.1" 200 OK
I believed that I had made something wrong, so I completely rewrote my entire app but got the same result, then I checked the requests and noticed that there's a small difference: The lack of the trailing slash at the bad request, this was generating the 404 not found error as
/socket.io/
is a different route regarding/socket.io
, so I tried the most straightforward idea, to force the Next.js rewrites to add the trailing slash but it seems like it's not possible, another thing I thought that could work would be to rewrite using the trailing slash at the end of my destination such as:But sadly this results in the rewrite not working and being more like a redirect to
https://localhost:8000/api/*/
. I know this is a Next.js problem and I'll open a issue there to enable the trailing slash in the rewriting, but I needed my software to work as soon as possible so I continued looking into this library and found that: It is not possible to create an endpoint without the trailing slash as this code found atasync_drivers/asgi.py
andmiddleware.py
don't allow it.I checked the official Socket.IO's documentation and found that adding the trailing slash when creating a server is an option that can be disabled, so I think it'd be good to have that option here too.
Describe the solution you'd like
I think that a good solution would be to create an optional add_trailing_slash parameter at the ASGIApp and WSGIApp init definitions, which default value can be True but can be changed to False in use cases like mine, I think that something like this would work:
I have already applied this solution on both, python-engineio and python-socketio in my forks and they seem to work well, I'll open the respective pull requests for this to be checked for the maintainer.
Describe alternatives you've considered
I considered rewriting my backend in JavaScript as the option exists in Socket.IO but that would take more time than just modifying this library a bit.
Additional context
This changes should also be applied to python-socketio for this option to work correctly.
The text was updated successfully, but these errors were encountered: