next-http-proxy-middleware icon indicating copy to clipboard operation
next-http-proxy-middleware copied to clipboard

WebSocket doesn't work with Next framework.

Open leonardossss opened this issue 2 years ago • 3 comments

I'm trying to use websocket with next-http-proxy-middleware, and it doesn't connect to my server, it just keeps the connection pending

next path: 'src/pages/api/[...all].ts'

import NextHttpProxyMiddleware from 'next-http-proxy-middleware'
import { NextApiRequest, NextApiResponse } from 'next/types'

export default (req: NextApiRequest, res: NextApiResponse) => NextHttpProxyMiddleware(req, res, {
    target: 'http://localhost:2000',
    ws: true
})

socket.io path: '/api/socket'

leonardossss avatar Jun 22 '22 19:06 leonardossss

I'm also having the same problem. Only polling works, ws or wss protocol doesn't work.

murilob1337 avatar Jun 22 '22 19:06 murilob1337

Hi @leonardossss

Unfortunately, Nextjs doesn't handle the ws(weboscket) scheme, so the client request cannot reach this middleware.

So, as @murilob1337 said, you should use HTTP polling (supported by some libraries like socket.io) instead of websockets.

Please let me know if I'm not understanding something

stegano avatar Jun 23 '22 01:06 stegano

Is there any way to do proxy websocket? , such as replacing the code in node_modules/next

StringKe avatar Jul 29 '22 03:07 StringKe

Any solution?

TopCommit avatar Nov 03 '22 20:11 TopCommit

@StringKe @TopCommit Hi there I checked your comment late. Sorry. Unfortunately, as far as I know, there is no official way to support Next.js.

stegano avatar Nov 04 '22 01:11 stegano