Skip to content

feat: add net.WebSocket - #52344

Merged
jkleinsc merged 2 commits into
43-x-yfrom
trop/43-x-y-bp-feat-add-net-websocket-1784055840012
Jul 17, 2026
Merged

jkleinsc merged 2 commits into
43-x-yfrom
trop/43-x-y-bp-feat-add-net-websocket-1784055840012

Conversation

@trop

@trop trop Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Backport of #51593

See that PR for details.

Notes: Added net.WebSocket, a WHATWG-compatible WebSocket client for the main process that routes through Chromium's network stack.

* feat: add net.WebSocket

Add a WHATWG-compatible WebSocket client to the net module, backed by
Chromium's network stack via NetworkContext::CreateWebSocket(). This
gives main-process code a WebSocket that goes through the session's
proxy configuration, certificate verification, and cookie store, with
the same drop-in API shape as the browser/Node global WebSocket.

The implementation has two pieces:

- shell/browser/api/electron_api_web_socket.{h,cc}: a gin-wrapped object
  that implements network.mojom.{WebSocketHandshakeClient, WebSocketClient,
  WebSocketAuthenticationHandler}, drives the mojo data pipes with
  back-pressure on both sides, reassembles fragmented frames, tracks
  bufferedAmount, and emits low-level events.
- lib/browser/api/net-websocket.ts: a class WebSocket extends EventTarget
  that implements the WHATWG surface (addEventListener, on{open,message,
  error,close}, readyState, bufferedAmount, protocol, extensions,
  binaryType, send(), close(), CONNECTING/OPEN/CLOSING/CLOSED) plus
  Electron-specific options (headers, origin, useSessionCookies, session,
  partition).

binaryType supports 'nodebuffer' (default), 'arraybuffer', and 'blob'.
useSessionCookies defaults to false, matching net.request(). The Origin
header defaults to the http(s) equivalent of the WebSocket URL's origin
so that the connection looks first-party for SameSite cookie purposes;
it can be overridden with the origin option.

Main process only for now; webRequest interception is a known follow-up.

* fix: address review findings and migrate WebSocketWrapper to gin::Wrappable

Migrate WebSocketWrapper from gin_helper::DeprecatedWrappable to
gin::Wrappable (cppgc-based, #47922):

- Add kElectronWebSocket wrappable pointer tag.
- Replace Pin()/Unpin() with gin_helper::SelfKeepAlive, cleared on teardown.
- Replace base::WeakPtrFactory with gin::WeakCellFactory + gin::WrapPersistent
  for callbacks; add Trace() to visit the weak cell.
- Annotate mojo::Receiver/Remote fields with GC_PLUGIN_IGNORE (browser process
  doesn't need execution-context tracking), matching UtilityProcessWrapper.
- Public constructor for cppgc::MakeGarbageCollected; Create() returns T*.

Spec and correctness fixes:

- Drop the unused WebSocketAuthenticationHandler. The 'login' event it emitted
  was never bridged to the WHATWG-only TS surface, leaving the mojo
  OnAuthRequired callback orphaned and the connection hung on a 401/407
  challenge until V8 GC. Pass NullRemote() so the network service fails the
  challenge fast (matching WebSocketConnectorImpl). Drop the 'authenticated
  proxies' claim from the docs.
- Serialize the send queue so a Blob send (which must read asynchronously)
  doesn't let a later string/ArrayBuffer send overtake it on the wire.
- Set readyState to CLOSED before dispatching the 'error' event, per the
  WebSocket spec ordering rules.
- Stop nulling the wrapper on close so bufferedAmount reflects untransmitted
  bytes (it must not reset to zero) and continues to count post-close sends.
- Use WebIDL [Clamp] semantics for the close() code argument.
- Disable permessage-deflate on the test ws server; it isn't the subject under
  test and ws@7's deflate close path can race a server-initiated close.

* chore: correct copyright holder in new files

* fix: address review feedback

- Track all bytes queued in the JS send chain (not just Blob reads) so
  bufferedAmount is accurate while a non-Blob send is waiting behind an
  asynchronous Blob read.
- Surface the network failure message and net error in CloseEvent.reason
  for connection failures (the WHATWG 'error' Event has no payload). Failure
  reasons from the network service already embed the net error name; only
  append it for internally-generated failures.
- Emit a Node Buffer from the C++ side via electron::Buffer::Copy() instead
  of a raw ArrayBuffer, avoiding a TS-side wrap on the default 'nodebuffer'
  path.
- Drop the redundant disconnect handler on the WebSocket remote; the
  WebSocketClient receiver and the remote terminate at the same WebSocketImpl
  in the network process.

* chore: remove accidentally committed lock file

Co-authored-by: Samuel Attard <[email protected]>
@trop
trop Bot requested a review from MarshallOfSound July 14, 2026 19:04
@trop trop Bot mentioned this pull request Jul 14, 2026
5 tasks done
@trop trop Bot added 43-x-y backport This is a backport PR backport/requested 🗳 semver/minor backwards-compatible functionality labels Jul 14, 2026
The cherry-pick missed the docs/api/structures/web-socket-options.md
entry in the auto docs list.

No-Verification-Needed: single regenerated build-manifest line; verified against generator output and by PR CI
@jkleinsc
jkleinsc merged commit 8d1cc07 into 43-x-y Jul 17, 2026
128 of 130 checks passed
@release-clerk

release-clerk Bot commented Jul 17, 2026

Copy link
Copy Markdown

Release Notes Persisted

Added net.WebSocket, a WHATWG-compatible WebSocket client for the main process that routes through Chromium's network stack.

@jkleinsc
jkleinsc deleted the trop/43-x-y-bp-feat-add-net-websocket-1784055840012 branch July 17, 2026 16:13
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Aug 2, 2026
##### [v43.2.0](https://github.com/electron/electron/releases/tag/v43.2.0)

### Release Notes for v43.2.0

#### Features

- Added `net.WebSocket`, a WHATWG-compatible WebSocket client for the main process that routes through Chromium's network stack. [#52344](electron/electron#52344) <sup>(Also in [42](https://github.com/electron/electron/pull/52345))</sup>

#### Fixes

- Fixed `app.disableHardwareAcceleration()` not fully disabling GPU hardware usage on Windows starting from Electron 38. [#52369](electron/electron#52369) <sup>(Also in [41](electron/electron#52368), [42](electron/electron#52371), [44](https://github.com/electron/electron/pull/52370))</sup>
- Fixed `globalShortcut` not working on GNOME Wayland by enabling Chromium's `GlobalShortcutsPortalPreferredTrigger` feature by default on Linux. [#52228](electron/electron#52228)
- Fixed a crash when showing Linux message boxes while another Linux UI implementation was active. [#52410](electron/electron#52410) <sup>(Also in [42](electron/electron#52408), [44](https://github.com/electron/electron/pull/52409))</sup>
- Fixed a window being marked hidden and background-throttled on macOS when covered by a transparent or click-through window. [#52389](electron/electron#52389) <sup>(Also in [44](https://github.com/electron/electron/pull/52391))</sup>
- Fixed unnecessary autofill popup creation for fields without datalist suggestions, which could cause input latency on macOS. [#52321](electron/electron#52321) <sup>(Also in [41](electron/electron#52320), [42](electron/electron#52319), [44](https://github.com/electron/electron/pull/52318))</sup>
- Fixed webContents.print() returning "Invalid printer settings" when print options are provided. [#52381](electron/electron#52381) <sup>(Also in [44](https://github.com/electron/electron/pull/52382))</sup>

#### Other Changes

- Updated Chromium to 150.0.7871.129. [#52355](electron/electron#52355)
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Aug 3, 2026
##### [v43.2.0](https://github.com/electron/electron/releases/tag/v43.2.0)

### Release Notes for v43.2.0

#### Features

- Added `net.WebSocket`, a WHATWG-compatible WebSocket client for the main process that routes through Chromium's network stack. [#52344](electron/electron#52344) <sup>(Also in [42](https://github.com/electron/electron/pull/52345))</sup>

#### Fixes

- Fixed `app.disableHardwareAcceleration()` not fully disabling GPU hardware usage on Windows starting from Electron 38. [#52369](electron/electron#52369) <sup>(Also in [41](electron/electron#52368), [42](electron/electron#52371), [44](https://github.com/electron/electron/pull/52370))</sup>
- Fixed `globalShortcut` not working on GNOME Wayland by enabling Chromium's `GlobalShortcutsPortalPreferredTrigger` feature by default on Linux. [#52228](electron/electron#52228)
- Fixed a crash when showing Linux message boxes while another Linux UI implementation was active. [#52410](electron/electron#52410) <sup>(Also in [42](electron/electron#52408), [44](https://github.com/electron/electron/pull/52409))</sup>
- Fixed a window being marked hidden and background-throttled on macOS when covered by a transparent or click-through window. [#52389](electron/electron#52389) <sup>(Also in [44](https://github.com/electron/electron/pull/52391))</sup>
- Fixed unnecessary autofill popup creation for fields without datalist suggestions, which could cause input latency on macOS. [#52321](electron/electron#52321) <sup>(Also in [41](electron/electron#52320), [42](electron/electron#52319), [44](https://github.com/electron/electron/pull/52318))</sup>
- Fixed webContents.print() returning "Invalid printer settings" when print options are provided. [#52381](electron/electron#52381) <sup>(Also in [44](https://github.com/electron/electron/pull/52382))</sup>

#### Other Changes

- Updated Chromium to 150.0.7871.129. [#52355](electron/electron#52355)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

43-x-y backport/approved ✅ backport This is a backport PR semver/minor backwards-compatible functionality

Projects

No open projects
Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants