question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

websocket proxy cannot upgrade websocket connection

See original GitHub issue

Expected behavior

HPM should upgrade websocket requests always

Actual behavior

HPM websocket proxy cannot upgrade websocket connections sporadically

Setup

  • http-proxy-middleware: 0.17.1
  • server: tornado 4.4.1 - Tornado Github
  • webpack: 1.13.2
  • webpack-dev-server: 1.16.1

proxy middleware configuration

 devServer: {
      proxy: {
        '/hyperguard/websocket/*': {
          target: 'ws://localhost:8082',
        ws: true
        }
      }
    }

server mounting

def serve_forever(self):
    app = self.__make_app()
    self.__http_server = HTTPServer(app)
    self.__http_server._handle_connection = self._handle_connection
    self.__http_server.listen(self.__port, self.__host)
    app.ssl_enabled = self.__ssl_options is not None
    self.__ioloop.start()

Howto Reproduce

http stream request

GET /hyperguard/websocket HTTP/1.1
Host: localhost:8079
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Sec-WebSocket-Version: 13
origin: http://localhost:8079
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: yrOcNHCvvVuGgttYgv9nzA==
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket

http stream response getting this response only sometimes, if not the websocket connection never be established

HTTP/1.1 101 Switching Protocols
upgrade: websocket
connection: Upgrade
sec-websocket-accept: I3u4B7iakSJWk4yLd02hfn+enus=
  • that issue also occurs on different platforms (fedora 22, ubuntu 14.04, centos7)
  • restarting webpack-dev-server doesnt change anything

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:20 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
torrejonvcommented, Nov 14, 2020

Apologies for commenting on an ancient ticket, but I managed to hit this issue (ws proxy only starts working after manually performing a GET call).

app.use(
    '/ws/live',
    createProxyMiddleware({
        target: process.env.API,
        ws: true, // enable websocket proxy
        changeOrigin: true,
        logLevel : 'debug'
    })
);

… “http-proxy-middleware”: “^1.0.6”, “react”: “^17.0.1”, “react-scripts”: “4.0.0”, …

I understand that no resolution was found, but does anyone know any workaround (better than manually doing a GET, I mean)?

6reactions
mixxencommented, Nov 28, 2016

I had problems with the proxy websocket closing right after connecting. Found out that socket io was closing unhandled requests. If you are using socket io, the fix is to set the destroyUpgrade option to false:

var express = require('express');
var app = express();
var http = require('http').Server(app);
var proxy = require('http-proxy-middleware');
var io = require('socket.io')(http, {destroyUpgrade: false});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to resolve the error: "Could not open websocket connection
How to resolve the error: “Could not open websocket connection: websocket: could not find connection header with token 'upgrade'”.
Read more >
Troubleshooting connection issues | Socket.IO
You are trying to reach a plain WebSocket server; The server is not reachable; The client is not compatible with the version of...
Read more >
Failed to upgrade websocket connection - Mattermost Forum
Select Reverse Proxy and press OK. 10.In the Add Reverse Proxy Rules dialog, 11.Enter 10.0.0.2:8065 in the Enter the server name or IP...
Read more >
java - Cannot connect to Spring WebSocket server due to null ...
Cannot connect to Spring WebSocket server due to null Upgrade Header · Can you use wss instead? · Changing to wss has the...
Read more >
Websockets problems — Dataiku DSS 11 documentation
The most common cause of Websocket error is when you connect to DSS through a proxy. Websockets is a fairly recent protocol and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found