RealtimeClient constructor
- String endPoint, {
- WebSocketTransport? transport,
- Duration timeout = Constants.defaultTimeout,
- int heartbeatIntervalMs = 30000,
- void logger()?,
- RealtimeEncode? encode,
- RealtimeDecode? decode,
- TimerCalculation? reconnectAfterMs,
- Map<
String, String> ? headers, - Map<
String, dynamic> params = const {}, - int longpollerTimeout = 20000,
- RealtimeLogLevel? logLevel,
- Client? httpClient,
Initializes the Socket
endPoint
The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol)
transport
The Websocket Transport, for example WebSocket.
timeout
The default timeout in milliseconds to trigger push timeouts.
params
The optional params to pass when connecting.
headers
The optional headers to pass when connecting.
heartbeatIntervalMs
The millisec interval to send a heartbeat message.
logger
The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log($kind: $msg
, data) }
encode
The function to encode outgoing messages. Defaults to JSON: (payload, callback) => callback(JSON.stringify(payload))
decode
The function to decode incoming messages. Defaults to JSON: (payload, callback) => callback(JSON.parse(payload))
longpollerTimeout
The maximum timeout of a long poll AJAX request. Defaults to 20s (double the server long poll timer).
reconnectAfterMs
The optional function that returns the millsec reconnect interval. Defaults to stepped backoff off.
Implementation
RealtimeClient(
String endPoint, {
WebSocketTransport? transport,
this.timeout = Constants.defaultTimeout,
this.heartbeatIntervalMs = 30000,
this.logger,
RealtimeEncode? encode,
RealtimeDecode? decode,
TimerCalculation? reconnectAfterMs,
Map<String, String>? headers,
this.params = const {},
this.longpollerTimeout = 20000,
RealtimeLogLevel? logLevel,
this.httpClient,
}) : endPoint = Uri.parse('$endPoint/${Transports.websocket}')
.replace(
queryParameters:
logLevel == null ? null : {'log_level': logLevel.name},
)
.toString(),
headers = {
...Constants.defaultHeaders,
if (headers != null) ...headers,
},
transport = transport ?? createWebSocketClient {
final customJWT = this.headers['Authorization']?.split(' ').last;
accessToken = customJWT ?? params['apikey'];
this.reconnectAfterMs =
reconnectAfterMs ?? RetryTimer.createRetryFunction();
this.encode = encode ??
(dynamic payload, Function(String result) callback) =>
callback(json.encode(payload));
this.decode = decode ??
(String payload, Function(dynamic result) callback) =>
callback(json.decode(payload));
reconnectTimer = RetryTimer(
() {
disconnect();
connect();
},
this.reconnectAfterMs,
);
}