adelriosantiago
adelriosantiago

Reputation: 8124

How to correctly setup devServer for WebSocket proxying in a vue-cli app

I have a Node.JS server running at http://localhost:8080 with 2 routes:

This server is also running a Websocket connection on the same port. Inside index.html I connect to the server with const socket = new WebSocket("ws://" + window.location.host).

When I run this server and visit http://localhost:8080 the following works:


Additionally, I am also running a vue-cli app at http://localhost:8079 in development mode with the following vue.config.js configuration:

...
devServer: {
  port: 8079,
  "/": {
    target: "http://localhost:8080/",
    ws: true
  },
}

When I run this app and visit http://localhost:8079 the following happens:


What I am doing wrong? How can I fix it?

As you can see I have included ws: true which is used to proxy WebSocket connections as well.

Upvotes: 8

Views: 7837

Answers (2)

Tip:

To get protocol/hostname/port from browser use

webSocketURL: 'auto://0.0.0.0:0/ws'

https://webpack.js.org/configuration/dev-server/#devserverserver

Upvotes: 0

Viktor Varga
Viktor Varga

Reputation: 59

I have the following in the vue.config.js:

  devServer: {
    proxy: {
      '^/': {
        target: 'http://localhost:8089',
        ws: true,
        changeOrigin: true
      }
    }
  }

The websocket backend runs on port 8089. All ws traffic proxied to the backend.

Upvotes: 5

Related Questions