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.

proxy to localhost ws/WebSocketServer results in Error: RSV1 must be clear

See original GitHub issue

Expected behavior

Proxying to the locally running WebSocketServer on a different port succeeds.

Actual behavior

Proxying to the locally running WebSocketServer on a different port fails with an error of; Error: RSV1 must be clear. This is internally thrown by ws because the buffer that the server is receiving begins with bytes that are technically invalid. See below for triage.

Setup

  • http-proxy-middleware: 0.17.4
  • server: express@latest
  • express: latest
  • ws: latest

proxy middleware configuration

const wsProxy = proxy('/', {
  target: 'http://localhost:9090',
  ws: true,
  logLevel: 'debug'
});

server mounting

const express = require('express')
const proxy = require('http-proxy-middleware');
const WebSocket = require('ws');

const wsProxy = proxy('/', {
  target: 'http://localhost:9090',
  ws: true,
  logLevel: 'debug'
});

const wss = new WebSocket.Server({ port: 9090 });

wss.on('connection', (socket) => {
  console.log('socket server connected');
  socket.on('message', (message) => {
    console.log('socket server message:', message);
    socket.send(`response: ${message}`);
  });
});

const app = express();
app.use('/', express.static(__dirname));
app.use(wsProxy);

const server = app.listen(8080, () => {

  const internalWss = new WebSocket.Server({ server });

  const client = new WebSocket('ws://localhost:8080');

  client.on('message', (message) => {
    console.log('client message:', message);
  });

  client.on('open', () => {
    console.log('client open');
    client.send('challenge');
  });
});

server.on('upgrade', wsProxy.upgrade);

This code block reproduces the issue with 100% of test runs locally here. If you comment out the line;

const internalWss = new WebSocket.Server({ server });

then the example runs successfully without error. There’s some kind of caveat/edge case with there being a WebSocketServer connected to the server on port 8080, as well as bound to localhost:9090. I did attempt to work my way through the http-proxy-middleware source to debug the cause but I came up empty. I also spent a fair amount of time within the source for ws to make sure that it wasn’t an error there; in which I couldn’t pinpoint a cause either. Given that the addition/removal of the WebSocketServer on port 8080 (same as the server) caused failure/pass, I figured I’d start with an issue here.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chimuraicommented, Oct 19, 2017

Thanks for the detailed report @shellscape. I’ll have to spend some time investigating this.

0reactions
shellscapecommented, Oct 21, 2017

@chimurai interesting. I wonder though if that setTimeout setup only succeeded without error because the internalWss wasn’t yet created and listening when the client message was sent.

Read more comments on GitHub >

github_iconTop Results From Across the Web

proxy to localhost ws/WebSocketServer results in Error: RSV1 ...
proxy to localhost ws/WebSocketServer results in Error: RSV1 must be clear.
Read more >
RangeError: Invalid WebSocket frame: RSV1 must be clear
someone keeps trying to crash my WebSocket server, and this error keeps getting sent when they try. Is there any possible fix to...
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