Opening a WebSocket stream over HTTP/2 with RFC 8441
The Extended CONNECT mechanism defined in 2018, examined with a loopback HTTP/2 negotiation and stream experiment.
Archive retrospective · Topic year 2018
In this article
A long-lived stream on a shared connection
A document editor opens with several jobs. It loads its application shell and the document, then listens for events produced by other editors. HTTP/2 can carry requests on separate streams over the same connection. The mechanism for starting a WebSocket depends on the HTTP version involved. RFC 8441, published in September 2018, defines Extended CONNECT as a way to start WebSocket on a single HTTP/2 stream. Ordinary resource requests on that session have lifetimes of their own. Closing a document's event stream is distinct from closing the connection. That distinction matters to client connection pools and to the logs produced by intermediaries. An operator can inspect settings negotiation and the stream's lifetime separately. This exercise focuses on those transitions, leaving the meaning of editing events to the application.
Waiting for the setting before constructing the request
Before sending Extended CONNECT, the client needs to see that its peer enabled SETTINGS_ENABLE_CONNECT_PROTOCOL. A successful HTTP/2 connection alone does not establish that capability. Waiting for the setting keeps the client from sending the extended form to a peer that did not advertise it. When enabled, the client uses CONNECT and identifies websocket through the protocol pseudo-header. The extended request also identifies its path, scheme, and authority. The server can therefore tell which application protocol a particular stream requests. A successful 2xx response accepts the stream. A monitor expecting only the familiar HTTP/1.1 status of 101 would miss this signal. When there is an intermediary, the advertised setting belongs to the peer the client sees. A separate test can examine what happens on the upstream leg.
What the local negotiation actually demonstrated
The example used Node.js v24.19.0 with a listener bound to 127.0.0.1 and an operating-system-assigned port. The client waited for the peer settings and asserted that enableConnectProtocol was true. It then opened a stream containing the method, protocol, and path for this exercise. The server sent a 200 response and a fifteen-byte test string. The client read the same payload and its assertions passed. The string is raw marker data. The exercise does not implement WebSocket framing, masking, Origin policy, TLS, or browser compatibility. It is a loopback demonstration of the HTTP/2 negotiation and successful stream traffic, with inputs under the tester's control. A transport test intended for deployment would add an actual WebSocket library after the stream opens and verify its frames and close handling.
import assert from 'node:assert/strict';
import { once } from 'node:events';
import { connect, createServer } from 'node:http2';
import { setTimeout } from 'node:timers/promises';
const payload = 'loopback stream';
const server = createServer({ settings: { enableConnectProtocol: true } });
server.on('stream', (stream, headers) => {
if (headers[':method'] !== 'CONNECT' || headers[':protocol'] !== 'websocket'
|| headers[':path'] !== '/local-demo') {
stream.respond({ ':status': 404 });
stream.end();
return;
}
stream.respond({ ':status': 200 });
stream.end(payload);
});
server.listen(0, '127.0.0.1');
await once(server, 'listening');
const client = connect('http://127.0.0.1:' + server.address().port);
try {
const [settings] = await once(client, 'remoteSettings');
assert.equal(settings.enableConnectProtocol, true);
const stream = client.request({
':method': 'CONNECT', ':protocol': 'websocket', ':scheme': 'http',
':authority': '127.0.0.1:' + server.address().port, ':path': '/local-demo'
});
stream.end();
const [response] = await once(stream, 'response');
assert.equal(response[':status'], 200);
let body = '';
for await (const chunk of stream) body += chunk;
assert.equal(body, payload);
console.log('enableConnectProtocol=true; HTTP/2 status=200; bytes=' + Buffer.byteLength(body));
} finally {
client.destroy();
server.close();
await setTimeout(10);
}
Looking for 101 and 200 on the right legs
An HTTP/1.1 Upgrade changes the protocol used on that connection. With Extended CONNECT, other HTTP/2 streams can coexist on the same connection. Accordingly, a success status needs to be read alongside the HTTP version. A proxy can use different approaches on the two legs, depending on its implementation and configuration. A working ordinary web page establishes little about timeout limits for long-lived streams. Logs can group opening failures by HTTP version, peer settings, status, and request path. An unavailable setting, a refused request, and an accepted stream that closes too soon are different stages. Measuring them independently helps engineers direct a change at the layer where the failure occurred. It also avoids treating an unrelated resource request as a failed WebSocket connection.
Testing the stream lifetime with the application
A second acceptance test for the editor could load the document on one stream while listening for updates on another. It can reveal whether a delayed large response affects the event channel. After the event stream closes, the application determines how to request updates again. A sequence number or known revision can supply a starting point. The transport does not select this application state automatically. Useful measurements include stream duration, close reason, reconnect count, and repeat delivery of a single editing event. The fifteen-byte local test says little about large-message flow control. A more realistic test can slow one consumer, close a stream partway through, and use an application assertion to check where the consumer resumes. This leaves behavior following reconnection visible even when the transport check succeeds.
Reading a 2018 standard using a current tool
September 2018 is the specification's date. The sample ran on the Node.js installation available today. Its output cannot establish what a particular historical browser did or what a production proxy will allow. Direct and proxied paths can each be exercised in a deployment test. First observe whether the peer offers the setting. Next see that a stream with the expected pseudo-headers is accepted. Finally verify that the chosen WebSocket libraries can exchange messages, cancel an operation, and resume the connection when appropriate. A failure report can record the HTTP version, the setting, the response on the stream, and the application event separately. Those observations separate what the standard defines, what the local run demonstrated, and what needs an environment-specific acceptance test. A useful follow-up also observes the rejection path. The example server returns 404 for an unknown path, but the recorded run exercises only the successful branch. Change only the path to isolate that difference, then run success and rejection on separate streams over the same session. The response to the unrecognized path should stay scoped to its stream. In an additional test where the setting is absent, verify that the client refrains from issuing Extended CONNECT. Any retry through HTTP/1.1 depends on the production client's supported fallback behavior.
Reader discussion
How would you measure this development's real-world impact?
No comments yet. Be the first to contribute.
Write a comment