cli: `nitro.devProxy` for websocket is not working

Environment

Nuxt 3.7

Reproduction

Nuxt 3.7

Describe the bug

Config:

nitro:{
  devProxy: {
    '/proxy': {
      target: `http://localhost:8080`,
      changeOrigin: true,
      ws: true,
    },
}

In web: image

image

image

If direct connect to Websocket Server: image

Additional context

No response

Logs

No response

About this issue

  • Original URL
  • State: open
  • Created 10 months ago
  • Reactions: 5
  • Comments: 16 (4 by maintainers)

Most upvoted comments

Can we proxy websockets still in Nuxt 3.11 when Nitro’s experimental websocket support is enabled? The above workaround is no longer working for me.

With #127 (v3.7.3), you can hook into the listen event to register a custom ws handler. Check out this example for usage.

Will be working on some upstream (nitro) fixes to fully unblock devProxy support as well 👍🏼

Thank you @pi0 for the information ! This help me find a workaround I use multiple Nuxt projects in iframe, in this case HMR does not work at all. @jktantan @ucw maybe can be useful for you and anyone in this case :

import { defineNuxtConfig } from "nuxt/config"
import { createProxyServer } from "httpxy"

export default defineNuxtConfig({
  nitro: {
    devProxy: {
      // Not working
      "/editor/assets/": {
        target: "ws://localhost:5003/editor/assets/",
        ws: true
      }
    }
  },
  hooks: {
    // Workaround
    listen(server) {
      const proxy = createProxyServer({ target: { host: "localhost", port: 5003 } })

      server.on("upgrade", (req, socket, head) => {
        if (req.url === "/editor/assets/") {
          proxy.ws(req, socket, head) // socket has type error by it works
        }
      })
    }
  }
})

@pi0 I will open a new issue to keep things clear here.

host+ssl -> #112