Skip to content

Conversation

@metcoder95
Copy link
Member

@metcoder95 metcoder95 commented Sep 11, 2025

This relates to...

Closes #2458

Rationale

Implements WebSocket over HTTP/2

Note

Experimental warning is emitted on first usage.

Changes

Features

Bug Fixes

Breaking Changes and Deprecations

Status

@KhafraDev
Copy link
Member

I started looking into it too... perfect timing

})
})

// TODO: ws does not supports HTTP2; will need to potentially write a custom server for this
Copy link
Member

@lpinca lpinca Sep 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See websockets/ws#1458 (comment) for a simple/limited wrapper.

@metcoder95
Copy link
Member Author

metcoder95 commented Sep 26, 2025

I found a bit of a wall here; for fetch, it seems the header expected is upgrade in order to understand the connection should be upgraded to a websocket. Nonetheless, this is not a valid H2 header, so we can just ack if we identify is an H2 connection or some other idea that we might have.

Ref: https://github.com/nodejs/undici/blob/be390d8cdd621a19da87ea65e0110f932394d72f/lib/web/websocket/connection.js

Happy to hear your thoughts @KhafraDev

@KhafraDev
Copy link
Member

We check for the header because it's needed to complete the http upgrade and opening handshake (RFC 6455). For http2, we should follow RFC 8441.

@metcoder95 metcoder95 marked this pull request as ready for review October 3, 2025 10:40
@metcoder95
Copy link
Member Author

@KhafraDev feel free to take another look

@metcoder95 metcoder95 requested a review from KhafraDev October 15, 2025 10:24
@codecov-commenter
Copy link

codecov-commenter commented Oct 15, 2025

Codecov Report

❌ Patch coverage is 91.62011% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.84%. Comparing base (2fa4db2) to head (b6c3895).

Files with missing lines Patch % Lines
lib/dispatcher/client-h2.js 92.46% 11 Missing ⚠️
lib/web/websocket/connection.js 87.50% 3 Missing ⚠️
lib/web/fetch/index.js 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4540      +/-   ##
==========================================
- Coverage   92.86%   92.84%   -0.02%     
==========================================
  Files         106      106              
  Lines       33111    33272     +161     
==========================================
+ Hits        30749    30892     +143     
- Misses       2362     2380      +18     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@metcoder95
Copy link
Member Author

@KhafraDev feel free to have another look to the docs and let me know your thoughts 👍

Copy link
Member

@KhafraDev KhafraDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a relevant test failure in test/http2-dispatcher.js

Comment on lines 41 to 43
> 🗒️ Note: WebSocket over HTTP/2 is possible to be enabled by default in further major versions,
> this will happen by enabling HTTP/2 connections as the de`fault behavior of Undici's Agent as well the global dispatcher.
> Stay tuned to the changelog for more information.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> 🗒️ Note: WebSocket over HTTP/2 is possible to be enabled by default in further major versions,
> this will happen by enabling HTTP/2 connections as the de`fault behavior of Undici's Agent as well the global dispatcher.
> Stay tuned to the changelog for more information.
> 🗒️ Note: WebSocket over HTTP/2 may be enabled by default in a future version,
> this will happen by enabling HTTP/2 connections as the default behavior of Undici's Agent as well the global dispatcher.
> Stay tuned to the changelog for more information.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the idea to enable http/2 by default outside of websocket? I was thinking of having it enabled only for websockets because I remember there was some push back last time you tried enabling it by default.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really sure what could be the plan here; the thing with enabling it only for WebSocket is that it might require to use a custom agent if H2 not enabled through global dispatcher as default.

It will be better to sync them, but if we are ok deviating one from the other, that should be totally fine

@metcoder95
Copy link
Member Author

@KhafraDev can you check? Let me know if is ok to merge it in this way

@metcoder95 metcoder95 requested a review from KhafraDev October 30, 2025 09:52
@KhafraDev
Copy link
Member

There are some http2 test failures, I'm not sure if they're relevant.

@metcoder95
Copy link
Member Author

Seems that they are now fixed, the failures doesn't seem related to the PR's changes

@metcoder95 metcoder95 merged commit 9271564 into main Oct 31, 2025
34 of 36 checks passed
@metcoder95 metcoder95 deleted the #2458 branch October 31, 2025 20:47
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

Successfully merging this pull request may close these issues.

websocket over http2

5 participants