Replies: 9 comments 16 replies
-
You can use http-proxy-middleware inside an API Route. This library supports proxying WebSockets if you enable the
|
Beta Was this translation helpful? Give feedback.
-
Does next.js now support websocket via api routes? Or does it require a http upgrade rather than ws protocol? |
Beta Was this translation helpful? Give feedback.
-
Something that never worked well in nextjs, very sad 😢 |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I'm able to get the WebSocket proxy working in dev mode but it seems to stop working when I deploy to production on my own server using the node.js runtime. Config looks like this: // eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require("path");
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
output: "standalone",
experimental: {
serverActions: true,
outputFileTracingRoot: path.join(__dirname, "../../"),
},
async rewrites() {
return [
{
source: "/api/:path*",
destination: `http://0.0.0.0:49153/:path*`,
},
];
},
};
module.exports = nextConfig; |
Beta Was this translation helpful? Give feedback.
-
Same issue here, and I also use the app router. Did anyone get it to work on the app router? |
Beta Was this translation helpful? Give feedback.
-
I have a Nest.JS back-end and I use socket.io. I got it working with the following code, I'm using app directory in Next.JS, but for proxy, I'm using pages directory because of http-proxy-middleware src/pages/api/proxy/[...paths.ts]
// some-provider-component.tsx
|
Beta Was this translation helpful? Give feedback.
-
![]() ![]() |
Beta Was this translation helpful? Give feedback.
-
Hi, been struggling with this for a while, now I got an implementation of a custom server that actually works, using express and http-proxy-middleware (couldnt make http-proxy properly works, for some reason couldnt get to upgrade connections correctly) server.js import express from 'express'
import { createProxyMiddleware } from 'http-proxy-middleware'
import next from 'next'
import { createServer } from 'http'
const dev = process.env.NODE_ENV !== 'production'
const hostname = process.env.HOSTNAME || 'localhost'
const port = process.env.PORT
const app = next({ dev, hostname, port })
const handle = app.getRequestHandler()
app
.prepare()
.then(() => {
const expressApp = express()
const server = createServer(expressApp)
expressApp.use(
'/api',
createProxyMiddleware({
target: 'https://www.server.com',
secure: false,
changeOrigin: true,
logLevel: 'debug',
ws: false,
})
)
// '/ws' prefix in my case
const wsProxy = createProxyMiddleware('/ws', {
target: 'wss://www.server.com',
changeOrigin: true,
ws: true,
secure: true,
logLevel: 'debug',
})
expressApp.use(wsProxy)
expressApp.all(/^\/_next\/webpack-hmr(\/.*)?/, async (req, res) => {
handle(req, res)
})
expressApp.all('*', (req, res) => handle(req, res))
server.on('upgrade', wsProxy.upgrade) //Properly upgrade connection
server.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`)
})
})
.catch(err => {
console.error('Server failed to start:', err)
})
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the feature you'd like to request
websocket proxy is needed for many people
Describe the solution you'd like
websocket proxy is needed for many people
Describe alternatives you've considered
websocket proxy is needed for many people
Beta Was this translation helpful? Give feedback.
All reactions