fix(mobile): iOS app gets compressed websocket frames #24931
main fromrefs/pull/24931/head·opened View on GitHubProblem#
The iOS app downloads every WebSocket frame uncompressed (#21183). The server has negotiated permessage-deflate since #4705, but only when the client offers it. On iOS the runtime uses React Native's global WebSocket, SocketRocket, which never sends Sec-WebSocket-Extensions. Android (OkHttp) and desktop (Chromium) offer it. The reporter measured the same providerStatuses push at 558 KB to an iPhone and 141 KB to desktop.
Change#
iOS now opens the app's WebSocket with URLSessionWebSocketTask, which offers permessage-deflate in its handshake.
apps/mobile/modules/t3-websocket: an Apple-only Expo module withconnect,sendText,sendBinaryandclose, and events keyed by a JS-assigned id. The session skips registeredURLProtocolclasses, since dev clients install ones that drop the socket after the 101. The receive limit is raised from URLSession's 1 MB default to 128 MB because thread snapshots can be larger. A task that ends without a close frame reports 1006, as browsers do, so the connection supervisor reconnects. One that ends cleanly withoutdidCloseWithreports 1005, so the JS socket never sticks in CLOSING.apps/mobile/src/lib/nativeWebSocket.ts: aSocket.WebSocketLikeover that module, provided as the mobile runtime'sWebSocketConstructor. A native open that arrives afterclose()is ignored, so an open timeout can't revive a dropped socket. The layer subscribes to native events when built and unsubscribes when released. Socket ids keep counting across Fast Refresh, so a late close from an older URLSession task can't close a newer socket.apps/mobile/src/lib/runtime.ts: uses that layer in place ofSocket.layerWebSocketConstructorGlobal.
Android has no T3WebSocket native module, and neither do iOS binaries built before this change, so both keep the global WebSocket unchanged. The constructor takes only a URL and subprotocols, which is all its two callers pass, and throws on other options rather than dropping them.
Scope and approval#
Fixes #21183. A maintainer's triage comment asked for an iOS capture of the upgrade request, and the reporter supplied it. #4794 was closed on the reading that #4705 covered it, but #4705 is the server half. There is no maintainer agreement on this approach yet. The choice for maintainers to confirm is a small app-owned native module for the main socket rather than a third-party WebSocket library. This awaits maintainer review.
Out of scope: the preview and device-stream sockets in client-runtime still use the global WebSocket.
Verification#
protectedData.test.ts, which builds the real mobile runtime, gains a check that iOS dials throughT3WebSocket. On main it fails at its assertion because the runtime hands iOS the globalWebSocket. Itsexpomock now answers per module name.nativeWebSocket.test.ts, 4 tests: text and binary round trip, server close codes, a late native open, and an old socket's close arriving after Fast Refresh opened its replacement.- Every mobile test file that imports or mocks
lib/runtimeor the new module passes: those two plusconnection,storage,preferences,pending-task-editor-writes,queued-run-edit,thread-outbox-removal,CloudAuthProvider,remoteRegistration,AddProjectScreen,thread-settings-sheet-stateandapp-updates. vp run typecheckinapps/mobile, plusvp fmt --checkandvp linton the changed files: clean.- iOS 26.5 simulator, Debug builds of main and this branch's first commit (the second changes no native code), each paired to a local server. tcpdump on the loopback port recorded the
/wsupgrade, query string dropped. On main the app's request has noSec-WebSocket-Extensionsand the 101 carries none. On this branch the request offerspermessage-deflateand the 101 accepts it. - On this branch the app sent a message over the new socket and the server's events came back on it. The provider error is expected: the test server had no provider.
Not checked:
- A physical iPhone, or a Release build.
- Bytes on the wire. The test server had almost no data, so the size difference is the reporter's measurement.
wssthrough T3 Connect or a reverse proxy, network changes, backgrounding, and frames over 1 MB.- Android on a device. Its path is unchanged: the module is Apple-only and the layer checks
Platform.OS.
Prior PRs#
- #4794 by t3dotgg built this module first and was closed in the backlog sweep, not on its merits. This PR keeps its Swift design, adds the clean-completion 1005 case, and rewrites the TypeScript side for Effect 4's
WebSocketLike. - #4705 by t3dotgg enabled permessage-deflate on the server.
- The diagnosis and both device captures in #21183 are by 1kaahu.
The upgrade headers the server saw from the iOS app on main (SocketRocket) and on this branch (URLSession).

This branch on the iOS simulator: a message sent over the new socket, and the server's reply events back on it.

The new runtime test on main: the iOS runtime dials through the global WebSocket.

The runtime test and the adapter tests passing on this branch.

Generated with Claude Opus 5.5 in T3 Code Nightly
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
No actionable comments were generated in the recent review.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: pingdotgg/t3code
- Branch: main
- File: .coderabbit.config.ts
- Commit: e762f09
- Layers: Repository TS config
- Review profile: CHILL
- Plan: Advanced
- Run ID:
662aac99-5c39-4d8f-966e-747cc31ee29e
Commits-
Reviewing files that changed from the base of the PR and between e72a8843b29976e7de07cd808552600e70a375c5 and 5318f29b5d45d48168d2a27638be598fae38b2ff.
Files selected for processing: 3-
apps/mobile/src/lib/nativeWebSocket.test.tsapps/mobile/src/lib/nativeWebSocket.tsapps/mobile/src/lib/protectedData.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.
-
Walkthrough
-
The PR adds an iOS Expo WebSocket module backed by
URLSessionWebSocketTaskand an Effect adapter. The runtime selects the native constructor when the module is available. Tests cover adapter behavior and runtime selection.Changes: iOS WebSocket
Layer / File(s) Summary Native WebSocket transport
apps/mobile/modules/t3-websocket/expo-module.config.json,apps/mobile/modules/t3-websocket/ios/T3WebSocket.podspec,apps/mobile/modules/t3-websocket/ios/T3WebSocketModule.swiftRegisters the Apple Expo module. The module manages URLSessionWebSocketTaskconnections, sends and receives text and binary messages, and emits connection events.Effect adapter and runtime integration
apps/mobile/src/lib/nativeWebSocket.ts,apps/mobile/src/lib/runtime.ts,apps/mobile/src/lib/nativeWebSocket.test.ts,apps/mobile/src/lib/protectedData.test.tsAdds an Effect adapter that routes native events and binary messages. The runtime selects the adapter when the native module is available. Tests cover adapter behavior and runtime selection. Estimated code review effort: 3 (Moderate) | ~25 minutes
Change: Bug fix · Severity of issue fixed: Medium
Sequence Diagram(s)#
sequenceDiagram participant Runtime participant NativeWebSocket participant T3WebSocketModule participant URLSessionWebSocketTask Runtime->>NativeWebSocket: Create socket with URL and protocols NativeWebSocket->>T3WebSocketModule: Connect with socket ID, URL, and protocols T3WebSocketModule->>URLSessionWebSocketTask: Resume connection URLSessionWebSocketTask-->>T3WebSocketModule: Deliver open, message, or close callback T3WebSocketModule-->>NativeWebSocket: Emit event with socket ID NativeWebSocket-->>Runtime: Dispatch WebSocket eventSuggested labels:
bugfix,📱 Native ChangeSuggested reviewers:
nekohasekai,juliusmarminge
Merge Risk:
Minimal · up to5318f-
No actionable issue is established for the iOS main-socket change; it is mergeable after normal checks.
Pre-merge checks:
4-
Check name Status Explanation Title check The title clearly identifies the main change: enabling compressed WebSocket frames in the iOS mobile app. Linked Issues check Issue #21183requires the iOS app's main WebSocket to offerpermessage-deflateand receive compressed frames. The PR adds the Apple-onlyT3WebSocketmodule withURLSessionWebSocketTask, wires it…Out of Scope Changes check The supplied change summary limits changes to the native WebSocket module, its Effect WebSocketLikeadapter, runtime wiring, and related tests. These changes support issue#21183. The PR leaves prev…Description check The description follows the required template. It explains the problem, implementation, scope, verification results, limitations, linked issue, screenshots, and agent model and harness. It also clearl… Full details: Linked Issues check
-
Explanation
Issue
#21183requires the iOS app's main WebSocket to offerpermessage-deflateand receive compressed frames. The PR adds the Apple-onlyT3WebSocketmodule withURLSessionWebSocketTask, wires it into the mobile runtime, and retains the globalWebSocketfallback for Android and older iOS binaries. The reported iOS simulator capture shows the request offeringpermessage-deflateand the server accepting it. Tests cover iOS runtime selection, text and binary frames, close handling, late open handling, and Fast Refresh socket replacement. The repository diff was unavailable for the current incremental comparison, so this conclusion uses the supplied PR summary and previous reviewed evidence.
Full details: Out of Scope Changes check
-
Explanation
The supplied change summary limits changes to the native WebSocket module, its Effect
WebSocketLikeadapter, runtime wiring, and related tests. These changes support issue#21183. The PR leaves preview and device-stream sockets inclient-runtimeunchanged, as required by the stated scope. No unrelated change is established. The repository diff was unavailable for the current incremental comparison, so this conclusion uses the supplied PR summary and previous reviewed evidence.
Full details: Description check
-
Explanation
The description follows the required template. It explains the problem, implementation, scope, verification results, limitations, linked issue, screenshots, and agent model and harness. It also clearly states that maintainer approval for the proposed approach is still pending.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
-
Note
Written by
gpt-6.1-solon behalf of Mariaupdated this branch directly, preserving @only21mil's commits and merging current main (
e762f09e265b). the original URLSession transport, native registration, fallback, and fast refresh design remain intact. no replacement pr or unrelated changes.added one production line: discard queued native messages once the socket leaves OPEN. a new regression in the existing adapter test failed before that guard and passes with it. added checks for layer teardown removing listeners and closing sockets, plus android and older-ios global-socket fallback. two independent reviews approved head
5318f29b5d45d48168d2a27638be598fae38b2ffwithout remaining findings.verification on blacksmith:
- current main fails the existing ios routing assertion because it calls the global websocket constructor.
- the candidate passes all 12 focused tests: routing/fallback, text/binary round trip, server close, late open, stale message, teardown, fast refresh, and protected-storage startup.
vp run check:changedpasses format, lint, both knip checks, mobile/scripts typecheck, and the two affected test files.
unverified: native ios compilation and actual compression negotiation, physical-device/release behavior, background/resume, network changes, proxy/T3 Connect, and large frames. this linux vm has no apple runtime. the original author's simulator captures remain author-reported evidence. no UI behavior or layout changed, so browser captures would not verify this transport.
maintainer approval is still needed for making the app-owned native transport the ios default. before merging, exercise the native app through background/resume and a network interruption, confirm reconnection delivers a new message, and check the negotiated extension on its
/wsupgrade. preview/device-stream sockets remain outside this fix, as in the original.Note
Written by
gpt-6.1-solon behalf of MariaVerified in a real dev environment#
disposition: unverifiable for the ios fix on this linux vm. these checks cover neighboring web behavior, not the changed native transport.
read the description, full diff, discussion, review comments, commit authors, and checks. the pr is open, with no commits by @juliusmarminge or @t3dotgg. checked exact head
5318f29b5d45d48168d2a27638be598fae38b2ffwithout changing it.- installed and ran
github/main(48a292364ab37574c67fb377b5227e5d281d9203) in/home/user/main, then stopped its captured server pid before starting the pr. installed the exact pr head and ran it with explicit isolated state:vp run dev --home-dir /home/user/t3code/.t3. - drove the actual web app with repository playwright and
/usr/bin/google-chrome. added a project pointing at the real/home/user/maingit repo, created a thread, selected codexgpt-6.1-sol, and submitted the same prompt on both revisions. both returnedtransport verification okthrough the real local provider proxy. no mocks, fixtures, or demo data. - on the pr, reloaded the client and saw persisted history. a fresh
/wsupgrade returned 101, offeringpermessage-deflate; client_max_window_bitsand acceptingpermessage-deflate. the web path also negotiated compression on main. - after browser offline/online, sent a follow-up in the same thread and received
reconnect verification ok. this does not establish that an underlying tcp connection dropped, or prove native reconnection. - inspected the exported clip, checked uploaded playback to the end and seeking in the t3 browser player, and verified the uploaded transcript. existing head checks are green where run; native preview and some deploy checks were skipped. no new source changes or check:changed run in this verification pass.
command and handshake transcript
Not verified#
the original ios bug could not be reproduced on main, and the changed ios behavior could not be exercised here. this linux x86_64 vm has no apple sdk, xcodebuild, swift toolchain, or native ios client. chrome uses its own websocket transport and never executes
T3WebSocketModule.swiftor the mobile runtime selection. a successful browser turn is therefore insufficient evidence for this fix.still required on an apple host: build and pair the native app on main and this head; capture the actual
/wsupgrade; confirm the main app does not offer compression and the pr does; run a real provider turn; exercise background/resume and a genuine connection interruption followed by another turn. native compilation, physical-device/release behavior, android and older-ios fallback, large frames, and t3 connect/proxy behavior remain unverified. no production fix was attempted without reproducing a failure in the changed path.- installed and ran
Note
🤖 Claude Opus 5.5 responding on behalf of Daniel Briskin
I ran this on a physical iPad. It negotiates compression and cuts the streaming data about 6x, but it didn't change whole-device power over Wi-Fi.
Setup: iPad mini (A17 Pro), iPadOS 26.7.2, Release build, server reached over Tailscale. The first runs were accidentally in landscape. I re-ran the power trial in portrait to match the #27325 bench, and the numbers below are from that run. I applied this PR's diff onto
dac8b11ddc, because that's where the #27325 power bench is pinned.- Works: the app connects, opens threads and streams a replayed turn normally.
- Bytes (measured in landscape; orientation doesn't change what the server sends): during the same 120 s streaming window, the server sent 122 KB to the app, against 755 KB without this PR. I counted the Tailscale extension's bytes on the server host.
- Power: a 2-block A/B trial on the streaming phase reads −25 / +7 mW per block in portrait, inside the bench's ~25 mW resolution. The landscape run read −6 / +1.
Not checked: reconnects, background/foreground, relay and tunnel paths, and cellular. Cellular is where fewer bytes should save the most power: in a sysdiagnose power log from the same user, about half of the non-display energy is cellular radio. This bench only uses Wi-Fi.
github-actions[bot](GitHub profile, opens in a new tab) added labels📱 Native Change
Thanks @only21mil, this was the right fix. While reworking https://git.cafe/pingdotgg/t3code/pulls/30732 we ended up building the same URLSession approach in https://git.cafe/pingdotgg/t3code/pulls/32288, which sits on top of https://git.cafe/pingdotgg/t3code/pulls/32287, and we're landing that one so the stack ships as a unit.
From your PR, it keeps:
- the
protocolClasses = []session, so dev-client URL protocols can't drop the socket - the 128 MiB receive limit
- the 1005/1006 close codes when a task ends without a close frame
It also adds:
- writes after close are dropped instead of throwing
- the close reason reaches the wire
- disposing the layer finalizes open sockets
- live byte counts on a pool iPhone, over ws and over wss through Tailscale Serve: about 8× smaller on the wire
Credit to you, to @t3dotgg for https://git.cafe/pingdotgg/t3code/pulls/4794, and to @1kaahu for the diagnosis in https://git.cafe/pingdotgg/t3code/issues/21183 is in that PR, which closes https://git.cafe/pingdotgg/t3code/issues/21183. Closing this one in favour of it.
- the
juliusmarminge closed this
Sign in to join this conversation.
Approvability#
Verdict: Needs Human Approval
Macroscope's review found this PR requires human approval — This PR adds a new iOS-native WebSocket stack and makes it the default transport for the mobile runtime, affecting production connection lifecycle and native build integration. The scope is broader than a self-contained bug fix and warrants maintainer review.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).