next.js: Rewrites are not working for websockets (socket.io)
What version of Next.js are you using?
10.0.8
What version of Node.js are you using?
v12.13.1
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
next dev (development/local)
Describe the Bug
The rewrite functionality works great, however it appears to not support websockets (more specifically socket.io). Below is my rewrite config:
async rewrites() {
return [
{
source: '/api/:slug*',
destination: 'http://localhost:3000/api/:slug*'
},
{
source: '/socket.io',
destination: 'http://localhost:3000'
}
]
}
The fact that websockets are not supported by the rewrites, forces me to use a custom server for development. Because the docs recommend not to use a custom server as it will result in a loss of optimizations, I’d rather not use one. If any workarounds exist that do not involve setting up a custom server, I’d love to hear them! 😄
Expected Behavior
Rewrites should support websockets, just as it support http requests
To Reproduce
See my rewrite config above.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 20
- Comments: 15 (6 by maintainers)
Commits related to this issue
- Enable websockets for rewrites proxy requests credit to https://github.com/vercel/next.js/issues/23147#issuecomment-801116463 — committed to ottob/next.js by ottob 5 years ago
- Enable websockets for rewrites proxy requests credit to https://github.com/vercel/next.js/issues/23147#issuecomment-801116463 — committed to ottob/next.js by ottob 5 years ago
- Enable websockets for rewrites proxy requests credit to https://github.com/vercel/next.js/issues/23147#issuecomment-801116463 — committed to ottob/next.js by ottob 5 years ago
Is anyone working on a fix? Not having a websockets proxy support in 2024 in the most popular server-side ReactJS framework is ridiculous…
I looked at the Next.js source code and I believe, that supporting web sockets would be really easy. It seems like all that is required is to add the following line to next-server.ts:
@JanKaifer Thanks for your quick comment!
I have done that by changing the
next-serversource code… It was just a quick and dirty check:I changed this line next-server.ts:698:
So not much of a viable workaround for others, I’m afraid…
@svetoslavstoyanov I ended up doing something similar. The problem with this solution though, is that it requires you to use a custom server. In production, I will use the default Next.js server to preserve all built-in optimizations. I prefer my production environment to be as similar as possible to my development environment to prevent any issues.
But thanks for the suggestion
@marnixhoh Yup, thanks for pinging this. I forgot to remove the label and it was autoclosed. I’ll ask relevant maintainers to help out here. But It might take a while since we have a lot of work with the app-directory and other new features.
I don’t think it should be closed, as it was tested against
next@canary. See this comment.As I indicated in the same comment, I would love to create a PR to fix this. However, I will need some guidance from the maintainers as to what direction to take (see the last paragraph of the comment).
Thanks!
Hi @JanKaifer, I spent the morning investigating this and trying to see if this issue has been resolved in
canaryor not.Since I opened this issue, my suggestion to add
ws: trueto theProxyconfig has already been implemented. Note that all #31515 proposed, was to add thisws: trueoption.Unfortunately, in the current
canaryversion ofNext, this turned out to not be enough to getsocket.iorewrites to work.So I dug a little deeper and I found the issue. The problem has to do with how
Nextformats thetargetof the rewrite rule. To getsocket.ioto work, the endpoint MUST have a trailing slash. E.g.:✅
http://localhost:3000/socket.io/❌http://localhost:3000/socket.ioUnfortunately, the
Next serverremoves this trailing slash, resulting in a404. I have tested forcing thetargetto have a trailing slash when it ends in/socket.ioand this does indeed solve the problem and causessocket.ioto work as expected.Few things to note:
trailingSlashoption, which can be provided tonext.config.jsas documented here, does not seem to have any effect on this particular issue. Even when combined with the guidance found in theRewritesdocumentation on trailing slashes:socket.ioto allow dropping the trailing slash: see hereHow to proceed from here? I’d be more than happy to work on a PR to make
Rewritescompatible withsocket.io. This would involve creating some way for the user to indicate that the trailing slash should not be removed from a providedtarget. Perhaps trailing slashes should never be removed from atargetor adding an additionalrewriteoption:keepTrailingSlashwould do the trick?Or of course, we can wait for the
socket.ioPR to go through.@JanKaifer I’m looking forward to hearing what your thoughts are on this and how I can help. 😃