Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions packages/core/realtime-js/src/RealtimeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
DEFAULT_TIMEOUT,
SOCKET_STATES,
TRANSPORTS,
VSN,
DEFAULT_VSN,
VSN_1_0_0,
VSN_2_0_0,
WS_CLOSE_NORMAL,
} from './lib/constants'

Expand Down Expand Up @@ -70,6 +72,7 @@ export type RealtimeClientOptions = {
timeout?: number
heartbeatIntervalMs?: number
heartbeatCallback?: (status: HeartbeatStatus) => void
vsn?: string
logger?: Function
encode?: Function
decode?: Function
Expand Down Expand Up @@ -109,6 +112,7 @@ export default class RealtimeClient {
heartbeatCallback: (status: HeartbeatStatus) => void = noop
ref: number = 0
reconnectTimer: Timer | null = null
vsn: string = DEFAULT_VSN
logger: Function = noop
logLevel?: LogLevel
encode!: Function
Expand Down Expand Up @@ -226,7 +230,7 @@ export default class RealtimeClient {
* @returns string The URL of the websocket.
*/
endpointURL(): string {
return this._appendParams(this.endPoint, Object.assign({}, this.params, { vsn: VSN }))
return this._appendParams(this.endPoint, Object.assign({}, this.params, { vsn: this.vsn }))
}

/**
Expand Down Expand Up @@ -825,6 +829,8 @@ export default class RealtimeClient {
this.worker = options?.worker ?? false
this.accessToken = options?.accessToken ?? null
this.heartbeatCallback = options?.heartbeatCallback ?? noop
this.vsn = options?.vsn ?? DEFAULT_VSN

// Handle special cases
if (options?.params) this.params = options.params
if (options?.logger) this.logger = options.logger
Expand All @@ -840,13 +846,27 @@ export default class RealtimeClient {
return RECONNECT_INTERVALS[tries - 1] || DEFAULT_RECONNECT_FALLBACK
})

this.encode =
options?.encode ??
((payload: JSON, callback: Function) => {
return callback(JSON.stringify(payload))
})
switch (this.vsn) {
case VSN_1_0_0:
this.encode =
options?.encode ??
((payload: JSON, callback: Function) => {
return callback(JSON.stringify(payload))
})

this.decode = options?.decode ?? this.serializer.decode.bind(this.serializer)
this.decode =
options?.decode ??
((payload: string, callback: Function) => {
return callback(JSON.parse(payload))
})
break
case VSN_2_0_0:
this.encode = options?.encode ?? this.serializer.encode.bind(this.serializer)
this.decode = options?.decode ?? this.serializer.decode.bind(this.serializer)
break
default:
throw new Error(`Unsupported serializer version: ${this.vsn}`)
}

// Handle worker setup
if (this.worker) {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/realtime-js/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { version } from './version'

export const DEFAULT_VERSION = `realtime-js/${version}`
export const VSN: string = '1.0.0'

export const VSN_1_0_0: string = '1.0.0'
export const VSN_2_0_0: string = '2.0.0'
export const DEFAULT_VSN: string = VSN_1_0_0

export const VERSION = version

Expand Down
Loading
Loading