next.js: WebSocket connection failed errors after upgrade to Next.js 12
What version of Next.js are you using?
12.0.1
What version of Node.js are you using?
12.22.3
What browser are you using?
Brave
What operating system are you using?
macOS
How are you deploying your application?
next start
Describe the Bug
After upgrading to Next.js 12, I’m getting the following error while running the app in dev mode:
WebSocket connection to 'wss://app.name.dev/_next/webpack-hmr' failed:
We’re using nginx to proxy the domain, so that we actually have a domain name, rather than just using localhost. I can’t think of any other gotchas that might be contributing factors.
The error repeats every few seconds, on a regular interval, and eventually floods the browser console. I am not seeing any errors printed to the terminal.
Expected Behavior
No errors!
To Reproduce
n/a
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 36
- Comments: 38 (8 by maintainers)
Commits related to this issue
- Add note about HMR WebSocket with express to upgrading (#30905) x-ref: https://github.com/vercel/next.js/issues/30491#issuecomment-957865735 ## Documentation / Examples - [x] Make sure the linting ... — committed to vercel/next.js by ijjk 3 years ago
- Fix broken HMR (https://github.com/vercel/next.js/issues/30491) — committed to Frojd/Wagtail-Pipit by rinti 2 years ago
- Add note about HMR WebSocket with express to upgrading (#30905) x-ref: https://github.com/vercel/next.js/issues/30491#issuecomment-957865735 ## Documentation / Examples - [x] Make sure the linting ... — committed to natew/next.js by ijjk 3 years ago
Hi, if you are using
nginxin front of Next.js during development you will need to configurenginxto pass the WebSocket request correctly. Related nginx documentation here http://nginx.org/en/docs/http/websocket.htmlFor me, this code worked:
where loadbalacer is local docker server.
FWIW I was also having this issue and @ijjk’s comment (https://github.com/vercel/next.js/issues/30491#issuecomment-953337935) put me in the correct direction. Previously there had not been a need to include the web sockets proxying for HMR to work but it seems now there is. After I added to proxying to the Next.JS server and restarted Nginx I no longer have this issue.
@matt-joecoffee We are also serving the site from a real domain in a similar way I suspect to you.
Obviously not helpful for non nginx situations like @pmoskvin’s but perhaps the package there is not forwarding these same headers and upgrading the http version?
I have this problem on my nextjs12. Running locally…no custom server…
I have the same problem. Nginx is not used

“next”: “^12.0.1” node v16.13.0
Maybe it’s a custom server? I use this package https://github.com/fridays/next-routes
hey there and the provided solution for routing all hmr requests to the nextjs handler is not working for me. The cause for it is, that i have an apollo server with websockets as well. So my custom server is defining a custom websocket route:
Searching for a solution…
I finally solved the issue I was having (see comment above). I use both a WS connection in my web app, which needs to be proxied in development, and Webpack HMR for development.
This is my modified custom server script (abbreviated):
Just to add, I was having this issue without any kind of middleware/proxy, I just restarted my computer to fix. Possible something WSL related.
@ijjk we’ll want to add this to the upgrading guide 👍
I have this same issue but I’m not using any customer server. Running Next.js 12 on Windows 11 with wsl2
For those using apache, here are the proxy settings you’ll need in your virtualhost, in this case I’m running node on port 10510, please adjust to suite your setup:
Ok, i fixed it simply with this:
be aware of https://github.com/vercel/next.js/issues/18080, if you use a custom
assetPrefixin dev mode, you need to wait for the next next.js version: #30632 (released in v12.0.3-canary.6)Sorry for coming back to this, but can anybody help find a solution when using a custom Express server?
As I understand, the problem here is slightly different, because the WS request cannot be forwarded/proxied to a HTTP port, because Next.js handles requests using a JavaScript function.
Example (abbreviated):