RealtimeClient constructor

RealtimeClient(
  1. String endPoint, {
  2. WebSocketTransport? transport,
  3. Duration timeout = Constants.defaultTimeout,
  4. int heartbeatIntervalMs = 30000,
  5. void logger(
    1. String? kind,
    2. String? msg,
    3. dynamic data
    )?,
  6. RealtimeEncode? encode,
  7. RealtimeDecode? decode,
  8. TimerCalculation? reconnectAfterMs,
  9. Map<String, String>? headers,
  10. Map<String, dynamic> params = const {},
  11. int longpollerTimeout = 20000,
  12. RealtimeLogLevel? logLevel,
  13. 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,
  );
}