Skip to content

nitro.devProxy for websocket is not working #107

Closed as duplicate
Closed as duplicate

Description

@jktantan

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

Activity

  1. github-actions commented on Aug 28, 2023

    @github-actions
    Contributor

    Would you be able to provide a reproduction? 🙏

    More info

    Why do I need to provide a reproduction?

    Reproductions make it possible for us to triage and fix issues quickly with a relatively small team. It helps us discover the source of the problem, and also can reveal assumptions you or we might be making.

    What will happen?

    If you've provided a reproduction, we'll remove the label and try to reproduce the issue. If we can, we'll mark it as a bug and prioritise it based on its severity and how many people we think it might affect.

    If needs reproduction labeled issues don't receive any substantial activity (e.g., new comments featuring a reproduction link), we'll close them. That's not because we don't care! At any point, feel free to comment with a reproduction and we'll reopen it.

    How can I create a reproduction?

    We have a couple of templates for starting with a minimal reproduction:

    👉 https://stackblitz.com/github/nuxt/starter/tree/v3-stackblitz
    👉 https://codesandbox.io/s/github/nuxt/starter/v3-codesandbox

    A public GitHub repository is also perfect. 👌

    Please ensure that the reproduction is as minimal as possible. See more details in our guide.

    You might also find these other articles interesting and/or helpful:

  2. ucw commented on Aug 28, 2023

    @ucw
  3. jktantan commented on Aug 28, 2023

    @jktantan
    Author

    nuxt/nuxt#15417

    Interesting, if I downgrade to NUXT 3.6.5, then your code is working. But I try to using your code for NUXT 3.7, that is not working.

    image

    image

    image

    image

  4. ucw commented on Aug 28, 2023

    @ucw

    It seems like this is related to how the new Nuxt CLI works, introduced in Nuxt 3.7. #101

  5. transferred this issue fromnuxt/nuxton Aug 28, 2023
  6. pi0 commented on Aug 28, 2023

    @pi0
    Member

    Thanks for the issue. I will be working on a quick solution to make it working again on nuxi 3.7 👍🏼

  7. self-assigned this
    on Aug 28, 2023
  8. vanling commented on Aug 28, 2023

    @vanling

    I hope when this is fixed the following works again

    {
      "name": "domain.nl",
      "private": true,
      "scripts": {
         "dev": "nuxt dev --host local.domain.nl --https --ssl-cert local.domain.nl.pem --ssl-key local.domain.nl-key.pem"
       }
    }

    package.json

  9. pi0 commented on Aug 28, 2023

    @pi0
    Member

    host issue is fixed already in latest 3.7.2 cli release. Can you please confirm that? 🙏

  10. vanling commented on Aug 28, 2023

    @vanling

    @pi0 Replacing nuxt dev with npx nuxi dev > Still getting the error

    - Node Version:     v18.16.1
    - Nuxt Version:     3.7.0
    - CLI Version:      3.7.2
    - Nitro Version:    2.6.2
    

    Mixed Content: The page at 'https://local.domain.nl:3000/ild' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://local.domain.nl:24678/_nuxt/'. This request has been blocked; this endpoint must be available over WSS.

    client.ts:69 [vite] failed to connect to websocket (SecurityError: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.).

    This worked in 3.6.5

  11. jktantan commented on Aug 28, 2023

    @jktantan
    Author

    host issue is fixed already in latest 3.7.2 cli release. Can you please confirm that? 🙏

    Unfortunately, it still doesn't work.

  12. pi0 commented on Aug 28, 2023

    @pi0
    Member

    @jktantan I meant the host. WebSocket issue is not resolved yet.

  13. vanling commented on Aug 28, 2023

    @vanling

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

    host+ssl -> #112

  14. pi0 commented on Aug 30, 2023

    @pi0
    Member

    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 👍🏼

  15. changed the title [-]nuxt3.7 devProxy for websocket not available[/-] [+]`nitro.devProxy` for websocket is not working[/+] on Aug 30, 2023
  16. 1 remaining item

  17. jenjen75 commented on Dec 11, 2023

    @jenjen75

    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
            }
          })
        }
      }
    })
  18. TimoStolz commented on Apr 11, 2024

    @TimoStolz

    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.

  19. emilecaron commented on Apr 23, 2024

    @emilecaron

    same thing here. workaround was fine before upgrading to 3.11

  20. 9M6 commented on Jul 8, 2024

    @9M6

    any news regarding this issue?

  21. danwritecode commented on Sep 24, 2024

    @danwritecode

    any update on this? this is holding up a production deployment

  22. yochze commented on Oct 11, 2024

    @yochze

    any update on this?

  23. zhaoyuqiqi commented on Oct 25, 2024

    @zhaoyuqiqi

    any update on this?

  24. emilecaron commented on Oct 25, 2024

    @emilecaron

    We eventually switched over to SEE which are working fine. But it won't suit every use case

  25. rubengees commented on Nov 8, 2024

    @rubengees

    For anybody still having problems with this, I found another workaround which works with the latest nuxt version (3.14.159 currently):

    import { defineNuxtModule } from "@nuxt/kit"
    import { createProxyServer } from "httpxy"
    
    // Custom nuxt module for adding websocket proxy support locally. The official support does not work yet,
    // see https://github.com/nuxt/cli/issues/107 and https://github.com/nuxt/cli/issues/108.
    export default defineNuxtModule({
      setup(_, nuxt) {
        if (!nuxt.options.dev) return
    
        // Take rules from the nitro devProxy configuration that have the ws flag set.
        const wsProxyRules = Object.entries(nuxt.options.nitro?.devProxy ?? {}).filter(
          ([, rule]) => typeof rule === "object" && rule.ws
        )
    
        // We have no rules with websocket proxying, return early.
        if (!wsProxyRules) return
    
        // Create proxy servers for each rule.
        const proxies = wsProxyRules.map(([key, rule]) => {
          return [key, createProxyServer(rule)]
        })
    
        nuxt.hook("ready", () => {
          // Replace the nuxt server with our own version that uses the configured proxy servers or delegates.
          nuxt.server = {
            ...nuxt.server,
            upgrade: (req, socket, head) => {
              const proxy = proxies.find(([key]) => req.url.startsWith(key))?.[1]
    
              if (proxy) {
                return proxy.ws(req, socket, {}, head)
              } else {
                return nuxt.server.upgrade(req, socket, head)
              }
            },
          }
        })
      },
    })

    Add it to e.g. modules/webSocketProxy.js and configure it as if devProxy would be working:

    nitro: {
      devProxy: {
        "/socket.io": {
          target: `http://localhost:8082`,
          ws: true,
        }
      }
    }
  26. danielroe commented on Jul 25, 2026

    @danielroe
    Member

    would you mind opening an issue upstream in nitro?

    tracked in nitrojs/nitro#4269

    as far as I can tell, a nitro.devProxy entry with ws: true never sees the upgrade: nitro's dev server registers devProxy routes only as HTTP handlers, while NitroDevServer.handleUpgrade forwards unconditionally to the dev worker, so nothing consults ws. HTTP requests to the same path proxy correctly.

    testing, the nuxt.server.upgrade workaround from this thread does still work on latest, and the bug still reproduces on nitro v3 so it isn't yet solved

    cc: @pi0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugnitroupstream

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions