Skip to content

Proxy error on second and further connections (express, http-proxy-middleware) #3062

Closed

Description

@ftes

You want to:

  • report a bug

Current behaviour

The first websocket connection after starting the server is fine.
The second and any later connection fails.

Error on server:

[HPM] Upgrading to WebSocket
[HPM] Error occurred while trying to proxy request /socket.io/?EIO=3&transport=websocket&sid=c6vi4Jz2M_NwmzSSAAAD from localhost:3000 to http://localhost:8885 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)

Error in Chrome:

websocket.js:112 WebSocket connection to 'ws://localhost:3000/socket.io/?EIO=3&transport=websocket&sid=c6vi4Jz2M_NwmzSSAAAD' failed: One or more reserved bits are on: reserved1 = 1, reserved2 = 0, reserved3 = 0

Error in Firefox:

The connection to http://localhost:3000/__webpack_hmr was interrupted while the page was loading.

Steps to reproduce (if the current behaviour is a bug)

  1. Define a websocket proxy with http-proxy-middleware: app.use('/__webpack_hmr', proxy({ws: true}));
  2. Integrate socket.io with express: require('socket.io')(server);
  3. Start two client sessions, one after the other
  4. Observe logs:
    a. The server handles the first websocket connection correctly.
    b. The second websocket connection seems to be delegated to the wildcard proxy.

Expected behaviour

Both client sessions manage to connect.

Setup

  • OS: arch linux
  • browser: Chrome, Firefox
  • socket.io version: 2.0.3

Other information (e.g. stacktraces, related issues, suggestions how to fix)

Fix: Pass the path to the proxy() call instead of app.use(). (see comment below)

Activity

  1. changed the title [-][Express] Proxy error on second and later connection[/-] [+][Express] Proxy error on second and further connections[/+] on Sep 26, 2017
  2. changed the title [-][Express] Proxy error on second and further connections[/-] [+][Express] Proxy error on second and further connections when using `.get('/*'`[/+] on Sep 26, 2017
  3. changed the title [-][Express] Proxy error on second and further connections when using `.get('/*'`[/-] [+][Express] Proxy error on second and further connections when using .get('/*'[/+] on Sep 26, 2017
  4. ftes commented on Sep 27, 2017

    @ftes
    Author

    Update: It seems this is rather an express issue.

    The problem was with a /__webpack_hmr proxy that also used a websocket.

    The following code "hijacked" the socket.io websocket and proxied it:

    const webpackHmrProxy = proxy({
        target: "http://mytarget",
        ws: true
    });
    
    app.use('/__webpack_hmr', webpackHmrProxy);

    The trick is to not rely on express to perform the path matching, and move the /__webpack_hmr context to the proxy call:

    const webpackHmrProxy = proxy('/__webpack_hmr', {
        target: "http://mytarget",
        ws: true
    });
    
    app.use(webpackHmrProxy);
  5. changed the title [-][Express] Proxy error on second and further connections when using .get('/*'[/-] [+]Proxy error on second and further connections (express, http-proxy-middleware)[/+] on Sep 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions