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 /**
@@ -825,6 +829,8 @@ export default class RealtimeClient {
825829 this . worker = options ?. worker ?? false
826830 this . accessToken = options ?. accessToken ?? null
827831 this . heartbeatCallback = options ?. heartbeatCallback ?? noop
832+ this . vsn = options ?. vsn ?? DEFAULT_VSN
833+
828834 // Handle special cases
829835 if ( options ?. params ) this . params = options . params
830836 if ( options ?. logger ) this . logger = options . logger
@@ -840,13 +846,27 @@ export default class RealtimeClient {
840846 return RECONNECT_INTERVALS [ tries - 1 ] || DEFAULT_RECONNECT_FALLBACK
841847 } )
842848
843- this . encode =
844- options ?. encode ??
845- ( ( payload : JSON , callback : Function ) => {
846- return callback ( JSON . stringify ( payload ) )
847- } )
849+ switch ( this . vsn ) {
850+ case VSN_1_0_0 :
851+ this . encode =
852+ options ?. encode ??
853+ ( ( payload : JSON , callback : Function ) => {
854+ return callback ( JSON . stringify ( payload ) )
855+ } )
848856
849- this . decode = options ?. decode ?? this . serializer . decode . bind ( this . serializer )
857+ this . decode =
858+ options ?. decode ??
859+ ( ( payload : string , callback : Function ) => {
860+ return callback ( JSON . parse ( payload ) )
861+ } )
862+ break
863+ case VSN_2_0_0 :
864+ this . encode = options ?. encode ?? this . serializer . encode . bind ( this . serializer )
865+ this . decode = options ?. decode ?? this . serializer . decode . bind ( this . serializer )
866+ break
867+ default :
868+ throw new Error ( `Unsupported serializer version: ${ this . vsn } ` )
869+ }
850870
851871 // Handle worker setup
852872 if ( this . worker ) {
0 commit comments