Skip to content

Commit 4589e34

Browse files
authored
feat(realtime): implement V2 serializer (#1829)
1 parent 2d862a1 commit 4589e34

File tree

6 files changed

+604
-55
lines changed

6 files changed

+604
-55
lines changed

packages/core/realtime-js/src/RealtimeClient.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
DEFAULT_TIMEOUT,
88
SOCKET_STATES,
99
TRANSPORTS,
10-
VSN,
10+
DEFAULT_VSN,
11+
VSN_1_0_0,
12+
VSN_2_0_0,
1113
WS_CLOSE_NORMAL,
1214
} from './lib/constants'
1315

@@ -70,6 +72,7 @@ export type RealtimeClientOptions = {
7072
timeout?: number
7173
heartbeatIntervalMs?: number
7274
heartbeatCallback?: (status: HeartbeatStatus) => void
75+
vsn?: string
7376
logger?: Function
7477
encode?: Function
7578
decode?: Function
@@ -109,6 +112,7 @@ export default class RealtimeClient {
109112
heartbeatCallback: (status: HeartbeatStatus) => void = noop
110113
ref: number = 0
111114
reconnectTimer: Timer | null = null
115+
vsn: string = DEFAULT_VSN
112116
logger: Function = noop
113117
logLevel?: LogLevel
114118
encode!: Function
@@ -226,7 +230,7 @@ export default class RealtimeClient {
226230
* @returns string The URL of the websocket.
227231
*/
228232
endpointURL(): string {
229-
return this._appendParams(this.endPoint, Object.assign({}, this.params, { vsn: VSN }))
233+
return this._appendParams(this.endPoint, Object.assign({}, this.params, { vsn: this.vsn }))
230234
}
231235

232236
/**
@@ -811,6 +815,8 @@ export default class RealtimeClient {
811815
this.worker = options?.worker ?? false
812816
this.accessToken = options?.accessToken ?? null
813817
this.heartbeatCallback = options?.heartbeatCallback ?? noop
818+
this.vsn = options?.vsn ?? DEFAULT_VSN
819+
814820
// Handle special cases
815821
if (options?.params) this.params = options.params
816822
if (options?.logger) this.logger = options.logger
@@ -826,13 +832,27 @@ export default class RealtimeClient {
826832
return RECONNECT_INTERVALS[tries - 1] || DEFAULT_RECONNECT_FALLBACK
827833
})
828834

829-
this.encode =
830-
options?.encode ??
831-
((payload: JSON, callback: Function) => {
832-
return callback(JSON.stringify(payload))
833-
})
835+
switch (this.vsn) {
836+
case VSN_1_0_0:
837+
this.encode =
838+
options?.encode ??
839+
((payload: JSON, callback: Function) => {
840+
return callback(JSON.stringify(payload))
841+
})
834842

835-
this.decode = options?.decode ?? this.serializer.decode.bind(this.serializer)
843+
this.decode =
844+
options?.decode ??
845+
((payload: string, callback: Function) => {
846+
return callback(JSON.parse(payload))
847+
})
848+
break
849+
case VSN_2_0_0:
850+
this.encode = options?.encode ?? this.serializer.encode.bind(this.serializer)
851+
this.decode = options?.decode ?? this.serializer.decode.bind(this.serializer)
852+
break
853+
default:
854+
throw new Error(`Unsupported serializer version: ${this.vsn}`)
855+
}
836856

837857
// Handle worker setup
838858
if (this.worker) {

packages/core/realtime-js/src/lib/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { version } from './version'
22

33
export const DEFAULT_VERSION = `realtime-js/${version}`
4-
export const VSN: string = '1.0.0'
4+
5+
export const VSN_1_0_0: string = '1.0.0'
6+
export const VSN_2_0_0: string = '2.0.0'
7+
export const DEFAULT_VSN: string = VSN_1_0_0
58

69
export const VERSION = version
710

0 commit comments

Comments
 (0)