perf(mobile): compressed websocket frames for iOS #4794
Closedt3dotgg(GitHub profile, opens in a new tab)wants to merge 5 commits into
main fromt3code/improve-mobile-data-loadingopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/mobile/modules/t3-websocket/ios/T3WebSocketModule.swift lines 180 to 192Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +155,21 @@155+ didOpenWithProtocol protocol: String?156+ ) {157+ queue.async {158+ guard let id = self.idsByTaskIdentifier[webSocketTask.taskIdentifier] else { return }159+ self.emit(id: id, event: "onOpen", payload: ["protocol": `protocol` ?? ""])160+ }161+ }162+163+ func urlSession(164+ _ session: URLSession,165+ webSocketTask: URLSessionWebSocketTask,166+ didCloseWith closeCode: URLSessionWebSocketTask.CloseCode,167+ reason: Data?168+ ) {169+ queue.async {170+ guard let id = self.idsByTaskIdentifier[webSocketTask.taskIdentifier] else { return }171+ let reasonText = reason.flatMap { String(data: $0, encoding: .utf8) } ?? ""172+ self.emit(id: id, event: "onClose", payload: ["code": closeCode.rawValue, "reason": reasonText])173+ self.remove(id: id, taskIdentifier: webSocketTask.taskIdentifier)174+ }175+ }apps/mobile/src/lib/nativeWebSocket.ts lines 103 to 103OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +83,21 @@83+84+class NativeWebSocket {85+ static readonly CONNECTING = 0;86+ static readonly OPEN = 1;87+ static readonly CLOSING = 2;88+ static readonly CLOSED = 3;89+90+ readyState: number = NativeWebSocket.CONNECTING;91+92+ private readonly id: number;93+ private readonly module: T3WebSocketNativeModule;94+ private readonly listeners = new Map<NativeWebSocketEventType, Set<ListenerEntry>>();95+96+ constructor(module: T3WebSocketNativeModule, url: string, protocols?: string | Array<string>) {97+ this.module = module;98+ this.id = nextSocketId++;99+ activeSockets.set(this.id, this);100+ attachNativeListeners(module);101+ const protocolList =102+ typeof protocols === "string" ? [protocols] : protocols ? [...protocols] : [];103+ module.connect(this.id, url, protocolList);
- apps/mobile/modules/t3-websocket/ios/T3WebSocketModule.swift line 159Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +122,21 @@122+ }123+ self.queue.async {124+ guard self.tasksById[id] != nil else { return }125+ self.receiveLoop(id: id, task: task)126+ }127+ case .failure:128+ // The delegate close/error callbacks own failure reporting; the loop129+ // just stops so it does not spin on a dead task.130+ break131+ }132+ }133+ }134+135+ private func emit(id: Int, event: String, payload: [String: Any]) {136+ queue.async {137+ guard let emitter = self.emittersById[id] else { return }138+ var enriched = payload139+ enriched["id"] = id140+ emitter(event, enriched)141+ }142+ }
- apps/mobile/modules/t3-websocket/ios/T3WebSocketModule.swift lines 113 to 126Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +94,21 @@94+ }95+ }96+97+ func close(id: Int, code: Int, reason: String) {98+ queue.async {99+ guard let task = self.tasksById[id] else { return }100+ let closeCode = URLSessionWebSocketTask.CloseCode(rawValue: code) ?? .normalClosure101+ task.cancel(with: closeCode, reason: reason.data(using: .utf8))102+ }103+ }104+105+ func cancelAll() {106+ queue.async {107+ for task in self.tasksById.values {108+ task.cancel(with: .goingAway, reason: nil)109+ }110+ self.tasksById.removeAll()111+ self.idsByTaskIdentifier.removeAll()112+ self.emittersById.removeAll()113+ }114+ }
- apps/mobile/src/lib/nativeWebSocket.ts line 163Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +135,21 @@135+ send(data: string | Uint8Array | ArrayBuffer): void {136+ if (typeof data === "string") {137+ this.module.sendText(this.id, data);138+ return;139+ }140+ const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);141+ this.module.sendBinary(this.id, bytesToBase64(bytes));142+ }143+144+ close(code?: number, reason?: string): void {145+ if (this.readyState === NativeWebSocket.CLOSING || this.readyState === NativeWebSocket.CLOSED) {146+ return;147+ }148+ this.readyState = NativeWebSocket.CLOSING;149+ this.module.close(this.id, code ?? 1000, reason ?? "");150+ }151+152+ handleOpen(): void {153+ this.readyState = NativeWebSocket.OPEN;154+ this.dispatch("open", { type: "open" });155+ }
- @@ -0,0 +43,21 @@43+let nextSocketId = 1;44+let nativeListenersAttached = false;45+46+function attachNativeListeners(module: T3WebSocketNativeModule) {47+ if (nativeListenersAttached) {48+ return;49+ }50+ nativeListenersAttached = true;51+ module.addListener("onOpen", (payload) => {52+ activeSockets.get(payload.id)?.handleOpen();53+ });54+ module.addListener("onMessage", (payload) => {55+ activeSockets.get(payload.id)?.handleMessage(payload.data ?? "", payload.binary === true);56+ });57+ module.addListener("onClose", (payload) => {58+ activeSockets.get(payload.id)?.handleClose(payload.code ?? 1006, payload.reason ?? "");59+ });60+ module.addListener("onError", (payload) => {61+ activeSockets.get(payload.id)?.handleError(payload.message ?? "WebSocket error");62+ });63+}
juliusmarminge closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
b9bf6195-023b-4445-b573-5bf5ad15a1f6You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share