Node: http2 websocket

Created on 6 Sep 2017  路  22Comments  路  Source: nodejs/node

Since http2 does not have the upgrade event maybe the docs should mention how to combine a node http2 server with websockets?

http2

Most helpful comment

The spec to enable websockets over http2 was just finished by the ietf working group in early September. Support for that just landed in nghttp2 about two weeks ago, and we've landed the basic mechanism in core just this week. I have a working prototype using the ws module. Progress is being made.

All 22 comments

Hi @ronag, you can't use websockets with http2. See https://daniel.haxx.se/blog/2016/06/15/no-websockets-over-http2/ for more info.

@apapirovski: Yes, so somehow I have to start a http1 server on the same port? How does e.g. nginx do it to support both on the same port?

http/2 does have a fallback mechanism that allows http1 and http2 on the same port; for right now the impl in core depends on having a TLS connection and using ALPN to differentiate. See the test case in https://github.com/nodejs/node/blob/master/test/parallel/test-http2-https-fallback.js

It's a bit rough still. We're still working through the details of this particular bit.

@jasnell: Thanks! Where can I follow your progress?

This repo is the best place. It's a bit difficult to follow at times, but if you track the PRs labeled http2 it should be fairly straightforward to follow along.

Would something like this work?

  // Check for websocket path
  if (headers[':path'].startsWith('/deepstream')) {
    const socket = net.connect(ds.port, ds.hostname, () => {
      stream.respond();
      socket.pipe(stream);
      stream.pipe(socket);
    })
  }

It should, yes.

wait... sorry, I got your threads mixed up... this should work for proxying in general, but not for websockets. There is no way to implement websockets on the same connection as http2

@jasnell: It would be nice to be able to have request listener that only receives the HTTP1 requests with no HTTP2 compat. I am handling HTTP2 in stream handler and the compat request instances are just wasted.

WOW. That's crazy: so today you have to choose whether use "http2" OR "websockets" with node?

There is a draft spec for using websockets with http2 but none of the existing implementations support it yet.

Thanks @jasnell, are you referring to this? https://tools.ietf.org/html/draft-ietf-httpbis-h2-websockets-00
It says:

This Internet-Draft will expire on June 22, 2018.

I'm pretty ignorant here, what's going to happen on June 22 this year?

All ietf internet draft versions have a six month life span to discourage things from becoming too stale. I would expect an updated draft on or before then

with spdy it works out of the box with https/2, websocket, it is werid nodejs does not support with http2 with websocket. we use socket.io for everything.

@p3x-robot https://github.com/nodejs/node/pull/23284, which was release in v10.12.0 today, allows just that :)

The spec to enable websockets over http2 was just finished by the ietf working group in early September. Support for that just landed in nghttp2 about two weeks ago, and we've landed the basic mechanism in core just this week. I have a working prototype using the ws module. Progress is being made.

ahh, awesome, i will finally i can delete spdy, testing and thanks you so much for your info!!!!

@jasnell will websocket over http2 have auto-reconnection logic built-in like SSE? Just wondering, because if they are using the same stream then this should be the case.

What is SSE here?

server side events

I use socket.io but it has reconnection logic not the stream..

@jasnell James could you provide us some recent material about how to use websockets over http2?

Was this page helpful?
0 / 5 - 0 ratings