Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP2 and WS Websocket fails #31695

Closed
najibghadri opened this issue Feb 8, 2020 · 4 comments
Closed

HTTP2 and WS Websocket fails #31695

najibghadri opened this issue Feb 8, 2020 · 4 comments

Comments

@najibghadri
Copy link

najibghadri commented Feb 8, 2020

  • Version: v10.16.0
  • Platform: 64-bit Windows 10
  • With self signed cert.
  • Client tester: used many, other node ws client code, socket testers, etc, not working.

I know about this issue 15230, and there is a supposed POC which is very hacky. I tried to set up an http2 server with ws websocket, like this:

const server = http2.createSecureServer({
  enableConnectProtocol: true,
  key: fs.readFileSync('localhost-privkey.pem'),
  cert: fs.readFileSync('localhost-cert.pem')
});

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

wss.on('connection', function connection(ws) {
  console.log('connected user');

  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('something');
});

server.listen(3000);

When I connect with a client to wss://localhost:3000 it fails.

error:

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:111:27)
Emitted 'error' event at:
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
npm ERR! code ELIFECYCLE
npm ERR! errno 1

Thank you!

@najibghadri
Copy link
Author

najibghadri commented Feb 8, 2020

If there is no solution or you say there isn't going to be a solution anytime soon could you suggest a way so that I can use http2 and websockets on one node server? Thank you!

@najibghadri najibghadri changed the title HTTP2 and WS Websocket HTTP2 and WS Websocket fails Feb 8, 2020
@lpinca
Copy link
Member

lpinca commented Feb 8, 2020

See websockets/ws#1458

@najibghadri
Copy link
Author

Thanks @lpinca !

@najibghadri
Copy link
Author

najibghadri commented Feb 8, 2020

Based on someone's comment on the thread linked above, "using node's http2 framework with the allowHTTP1: true option then setting a ws server to listen on the http2 server just works."
Worked for me too, websocket connections builds up, http api is on http2 so, looks ok, idk how it works and if this is a correct long-term solution but I will keep using it for now. If I will have problems I will separate the server into HTTP2 and HTTPS WSS servers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants