websockets / ws

Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js
MIT License
21.32k stars 2.3k forks source link

WebSocket over HTTP2 #1458

Open rahbari opened 5 years ago

rahbari commented 5 years ago

Now that node core has added websocket support over http2 https://github.com/nodejs/node/pull/23284, It's nice to know when it will be added to WS?

lpinca commented 5 years ago

See https://github.com/nodejs/node/pull/23284#discussion_r223162692. To answer your question, when someone interested in having the feature, will implement it and open a PR.

ayZagen commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

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 commented 4 years ago

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

josephg commented 4 years ago

@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 commented 4 years ago

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 https://github.com/aspnet/AspNetCore/issues/7801 and in particular this comment https://github.com/aspnet/AspNetCore/issues/7801#issuecomment-486498006.

FallingSnow commented 4 years ago

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

damianobarbati commented 4 years ago

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

LongTengDao commented 4 years ago

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 commented 4 years ago

Do any browsers support "websocket over http2" now?

adelyte-chris commented 4 years ago

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

lpinca commented 4 years ago

@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 commented 4 years ago

@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 commented 3 years ago

Do any browsers support "websocket over http2" now?

No

StEvUgnIn commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

szmarczak commented 3 years ago

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

piranna commented 1 year ago

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

esinanturan commented 1 year ago

Any plans for supporting it in this library ?

CMCDragonkai commented 11 months ago

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 commented 6 months ago

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 commented 6 months ago

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

ackava commented 4 months ago

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 commented 4 months ago

@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 commented 4 months ago

@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 commented 3 months ago

any update?