Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit 64c42b8

Browse files
authored
fix: expo compatibility layer (#466)
1 parent 3c5ea35 commit 64c42b8

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,15 @@
6262
"typescript": "^5.7.3",
6363
"vitest": "^2.1.9",
6464
"web-worker": "1.2.0"
65+
},
66+
"exports": {
67+
".": {
68+
"import": "./dist/module/index.js",
69+
"require": "./dist/main/index.js"
70+
},
71+
"./websocket": {
72+
"node": "./src/node.js",
73+
"default": "./src/native.js"
74+
}
6575
}
6676
}

src/RealtimeClient.ts

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { WebSocket as WSWebSocket } from 'ws'
1+
import WebSocket from './WebSocket'
22

33
import {
44
CHANNEL_EVENTS,
@@ -54,7 +54,7 @@ export interface WebSocketLikeConstructor {
5454
): WebSocketLike
5555
}
5656

57-
export type WebSocketLike = WebSocket | WSWebSocket | WSWebSocketDummy
57+
export type WebSocketLike = WebSocket | WSWebSocketDummy
5858

5959
export interface WebSocketLikeError {
6060
error: any
@@ -81,13 +81,13 @@ export type RealtimeClientOptions = {
8181
accessToken?: () => Promise<string | null>
8282
}
8383

84-
const NATIVE_WEBSOCKET_AVAILABLE = typeof WebSocket !== 'undefined'
8584
const WORKER_SCRIPT = `
8685
addEventListener("message", (e) => {
8786
if (e.data.event === "start") {
8887
setInterval(() => postMessage({ event: "keepAlive" }), e.data.interval);
8988
}
9089
});`
90+
9191
export default class RealtimeClient {
9292
accessTokenValue: string | null = null
9393
apiKey: string | null = null
@@ -209,33 +209,21 @@ export default class RealtimeClient {
209209
if (this.conn) {
210210
return
211211
}
212-
212+
if (!this.transport) {
213+
this.transport = WebSocket
214+
}
213215
if (this.transport) {
214216
this.conn = new this.transport(this.endpointURL(), undefined, {
215217
headers: this.headers,
216218
})
217219
this.setupConnection()
218220
return
219221
}
220-
221-
if (NATIVE_WEBSOCKET_AVAILABLE) {
222-
this.conn = new WebSocket(this.endpointURL())
223-
this.setupConnection()
224-
return
225-
}
226-
227222
this.conn = new WSWebSocketDummy(this.endpointURL(), undefined, {
228223
close: () => {
229224
this.conn = null
230225
},
231226
})
232-
233-
import('ws').then(({ default: WS }) => {
234-
this.conn = new WS(this.endpointURL(), undefined, {
235-
headers: this.headers,
236-
})
237-
this.setupConnection()
238-
})
239227
}
240228

241229
/**
@@ -560,7 +548,7 @@ export default class RealtimeClient {
560548
}
561549

562550
/** @internal */
563-
private async _onConnOpen() {
551+
private _onConnOpen() {
564552
this.log('transport', `connected to ${this.endpointURL()}`)
565553
this.flushSendBuffer()
566554
this.reconnectTimer.reset()
@@ -576,11 +564,10 @@ export default class RealtimeClient {
576564
} else {
577565
this.log('worker', `starting default worker`)
578566
}
579-
580567
const objectUrl = this._workerObjectUrl(this.workerUrl!)
581568
this.workerRef = new Worker(objectUrl)
582569
this.workerRef.onerror = (error) => {
583-
this.log('worker', 'worker error', error.message)
570+
this.log('worker', 'worker error', (error as ErrorEvent).message)
584571
this.workerRef!.terminate()
585572
}
586573
this.workerRef.onmessage = (event) => {
@@ -593,12 +580,10 @@ export default class RealtimeClient {
593580
interval: this.heartbeatIntervalMs,
594581
})
595582
}
596-
597-
this.stateChangeCallbacks.open.forEach((callback) => callback())!
583+
this.stateChangeCallbacks.open.forEach((callback) => callback())
598584
}
599585

600586
/** @internal */
601-
602587
private _onConnClose(event: any) {
603588
this.log('transport', 'close', event)
604589
this._triggerChanError()
@@ -631,7 +616,6 @@ export default class RealtimeClient {
631616
}
632617
const prefix = url.match(/\?/) ? '&' : '?'
633618
const query = new URLSearchParams(params)
634-
635619
return `${url}${prefix}${query}`
636620
}
637621

src/WebSocket.native.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Native/browser WebSocket entry point
2+
const NativeWebSocket = typeof WebSocket !== 'undefined' ? WebSocket : undefined
3+
4+
export default NativeWebSocket

src/WebSocket.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Node.js WebSocket entry point
2+
import WebSocket from 'ws'
3+
4+
export default WebSocket

0 commit comments

Comments
 (0)