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

WebSocket over HTTP2 #1458

Open
rahbari opened this issue Oct 15, 2018 · 28 comments
Open

WebSocket over HTTP2 #1458

rahbari opened this issue Oct 15, 2018 · 28 comments

Comments

@rahbari
Copy link

rahbari commented Oct 15, 2018

Now that node core has added websocket support over http2 nodejs/node#23284, It's nice to know when it will be added to WS?

@lpinca
Copy link
Member

lpinca commented Oct 15, 2018

See nodejs/node#23284 (comment). To answer your question, when someone interested in having the feature, will implement it and open a PR.

@ayZagen
Copy link

ayZagen commented Dec 25, 2018

@lpinca We rely on ws and want to upgrade our servers to http2. It would be great to have http2 support on ws.
Thanks

@lpinca
Copy link
Member

lpinca commented Dec 25, 2018

@ayZagen you are welcome to experiment with it. ws is built on top of the upgrade mechanism for HTTP/1.1 and works with a raw (net.Socket) TCP socket.

The way WebSocket works over HTTP/2 is different, the HTTP/2 API is different so it's not trivial to add support for it. Here is a proof of concept: https://github.com/jasnell/http2ws/blob/master/http2ws.js. It might be easier to write a wrapper module with the eventual required additions in ws instead of adding support directly in ws.

I am personally not interested in having HTTP/2 support because I think there is no real benefit in using an HTTP/2 stream instead of a raw TCP socket as the transport layer. I understand that in some cases it might be desirable to support HTTP/2 and WebSocket on the same server but I think that in many cases having a separate HTTP/1.1 server only for WebSocket is a good and viable option.

@FallingSnow
Copy link

I'm using node's http2 framework with the allowHTTP1: true option. Then setting a ws server to listen on the http2 server just works.

Although I would like to see ws support for HTTP2 so it feels less hacky.

@josephg
Copy link

josephg commented Jul 18, 2019

@FallingSnow does that do websockets over http2 or is that just falling back to websockets over http1.1?

@josephg
Copy link

josephg commented Jul 18, 2019

@lpinca the big advantage (architecturally) is taking advantage of multiplexing. Right now using websockets sort of requires you to throw away any http-like abstractions (and indeed we often just open a single ws connection to ws://example.com/ws or something) and do our own multiplexing.

With ws over h2 it can start making sense again for applications / APIs to add per-URL websocket connections for specific change feeds / use cases. (Since all connections are multiplexed through the single h2 tcp socket anyway).

The other advantage is faster connection times, since if the page is loaded over h2 then we don't need to do another tcp/TLS handshake to open a websocket.

@lpinca
Copy link
Member

lpinca commented Jul 18, 2019

WebSocket over HTTP2 has only one advantage that is as you said the use of a single TCP connection when the origin is the same. In all other cases there are only disadvantages especially on the server and with the Node.js HTTP2 implementation.

Node.js HTTP2 adds a lot of overhead over a plain {net,tls}.Socket. There are streams wrapping other streams and the whole HTTP2 machinery. I did not run any benchmark but I'm pretty sure it would perform way worse in terms of both speed and especially memory usage.

See also this discussion dotnet/aspnetcore#7801 and in particular this comment dotnet/aspnetcore#7801 (comment).

@FallingSnow
Copy link

FallingSnow commented Jul 20, 2019

@josephg I believe the connection comes in as an HTTP1.1 request. There is no actual HTTP2 as far as I'm aware.

@damianobarbati
Copy link

Check this out guys: https://nodejs.org/api/http2.html section "The Extended CONNECT Protocol"

@LongTengDao
Copy link
Contributor

LongTengDao commented Apr 8, 2020

By read all source code of ws (as server side), and find each place using socket, it seems that we only need do small change to support HTTP2:

  1. socket.write('HTTP/1.1 101 Switching Protocols\r\n...') change to socketOrStream.additionalHeaders ? socketOrStream.additionalHeaders({ ':status': 101, ... }) : socketOrStream.write('...');
  2. socket.setNoDelay(); change to socketOrStream.setNoDelay && socketOrStream.setNoDelay();;
  3. socket.on data end error close change to socketOrStream.on data end error close frameError aborted, and remember remove them at right time.

Because net.Socket and (Server)Http2Stream are both stream.Duplex.

@masx200
Copy link

masx200 commented Apr 24, 2020

Do any browsers support "websocket over http2" now?

@adelyte-chris
Copy link

HTTP/2 explicitly disallows WebSocket upgrades. See HTTP/2 Issue #386 for the current status of adding WebSockets semantics to HTTP/2.

@lpinca
Copy link
Member

lpinca commented May 13, 2020

@adelyte-chris RFC 8441 describes how to run the WebSocket protocol over a single stream of an HTTP/2 connection and this is already supported by Node.js.

That said, I did not change my mind on this topic.

@adelyte-chris
Copy link

@lpinca My mistake, I saw the draft status of the RFC linked in the issue and didn't follow up to see if it had progressed.

@masx200 Chromium has support for WebSockets over HTTP/2 behind a command line flag. I found a reference to Firefox support being in development but can't find a tracking issue for it.

@note8g2018
Copy link

Do any browsers support "websocket over http2" now?

No

@StEvUgnIn
Copy link

StEvUgnIn commented Aug 31, 2020

Do any browsers support "websocket over http2" now?

No

We should make a petition to Google and W3C... This is very concerning for Q3 2020...

@note8g2018
Copy link

note8g2018 commented Aug 31, 2020

Do any browsers support "websocket over http2" now?

No

We should make a petition to Google and W3C... This is very concerning for Q3 2020...

I do not think anybody will do websocket over HTTP2 because they waiting for HTTP3, which it should be done at this time, but they found a problem I think, and it may be will be very late.

by the way, it is better to make two software web-server, one for HTTP1.1 or HTTP2, and the second for websocket.
and they can run simultaneously in VPS by pm2 for example

@luncheon
Copy link

FYI: Websockets over HTTP/2 will be supported (enabled by default) in Chrome 91
https://www.chromestatus.com/feature/6251293127475200

@szmarczak
Copy link

Here's a client example using http2-wrapper: https://github.com/szmarczak/http2-wrapper/blob/master/examples/ws/index.js

@piranna
Copy link

piranna commented Jan 11, 2023

Chrome 95 already supports that: https://chromestatus.com/feature/6251293127475200

@esinanturan
Copy link

Any plans for supporting it in this library ?

@CMCDragonkai
Copy link

Here's a client example using http2-wrapper: https://github.com/szmarczak/http2-wrapper/blob/master/examples/ws/index.js

@szmarczak it looks like you've managed to implement websockets over HTTP2 on both client side and server side using node's http2 module and this ws library. Is that production ready or are there other things that is still needed for full websocket over http2 support? (Like ping/pong... etc).

Also why did you do this:

ws.setSocket(stream, head, 100 * 1024 * 1024);

What is the 100 * 1024 * 1024?

@brentmjohnson
Copy link

Use of ws server-side seems to work fine as-is with a native chromium WebSocket API client. HTTP/1 requests hit the "upgrade" event and HTTP/2 requests hit the "connect" event.

Only thing I could imagine adding to ws might be a convenience handleConnect method to the server class, but that saves what - like 15 LOC?

Node.js native http2 from "node:http2" for v20.10.0:

const server = http2.createSecureServer(
  {
    key: syncfs.readFileSync("localhost-privkey.pem"),
    cert: syncfs.readFileSync("localhost-cert.pem"),
    allowHTTP1: true,
    settings: {
      enableConnectProtocol: true,
    }
  }
);

const wsServer = new WebSocket.Server({ noServer: true });

const onConnectUpgrade = (ws, request) => {
  wsServer.emit("connection", ws, request);
}

server.on("upgrade", (request, socket, head) => {
  wsServer.handleUpgrade(request, socket, head, (ws) => onConnectUpgrade(ws, request));
});

server.on('connect', (request, response) => {
  if (request.headers[':protocol'] === 'websocket' &&
    request.headers[':method'] === 'CONNECT') {
    response.stream.respond({
      ':status': 200
    });

    const ws = new WebSocket(null);
    ws.setSocket(response.stream, Buffer.alloc(0), {
      maxPayload: 104857600,
      skipUTF8Validation: false,
    });

    onConnectUpgrade(ws, request);
  }
});

wsServer.on("connection", (socket, request) => {
  // do something with your new websocket!
})

Thanks for a great library!

@lpinca
Copy link
Member

lpinca commented Dec 15, 2023

@brentmjohnson your example only covers the server side and skipping wsServer.handleUpgrade() in the http2 case means ignoring

  • handshake validation.
  • hooks (verifyClient, 'headers' event, etc.).
  • permessage-deflate negotiation.
  • maybe other things I can't think of at the moment.

@ackava
Copy link

ackava commented Feb 14, 2024

Chrome 121 onwards sends websocket over http/2, so WebSocket over HTTP2 is now kind of mandatory.

@brentmjohnson your method works, however the socket is closed by Chrome browser after few seconds and it reconnects the server back again.

@brentmjohnson
Copy link

@ackava, i believe most browsers should respect the server's ALPN negotiation, but yeah if your server reports http2 and support for extended connect (re: RFC 8441) i would expect chrome to try websocket over http2.

i haven't seen that disconnect issue on chrome 121.0.6167.185 or edge 123.0.2406.0. are you getting anything in the browser console, or perhaps have a proxy between the browser and the server?

@ackava
Copy link

ackava commented Feb 28, 2024

@brentmjohnson Please Disregard my comment, the issue is with engine.io not handling connect event, your workaround does work correctly. I am currently using your work around to connect to socket.io over http1 and proxy the messages (yes overkilling CPU).

@masx200
Copy link

masx200 commented Mar 29, 2024

any update?

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