RealtimeClient constructor

RealtimeClient(
  1. String endPoint, {
  2. WebSocketChannel transport(
    1. String url,
    2. Map<String, String> headers
    )?,
  3. void encode(
    1. dynamic payload,
    2. void callback(
      1. String result
      )
    )?,
  4. void decode(
    1. String payload,
    2. void callback(
      1. dynamic result
      )
    )?,
  5. Duration timeout = Constants.defaultTimeout,
  6. int heartbeatIntervalMs = 30000,
  7. int longpollerTimeout = 20000,
  8. TimerCalculation? reconnectAfterMs,
  9. void logger(
    1. String? kind,
    2. String? msg,
    3. dynamic data
    )?,
  10. Map<String, String> params = const {},
  11. Map<String, String>? headers,
})

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, {
  WebSocketChannel Function(String url, Map<String, String> headers)?
      transport,
  void Function(dynamic payload, void Function(String result) callback)?
      encode,
  void Function(String payload, void Function(dynamic result) callback)?
      decode,
  this.timeout = Constants.defaultTimeout,
  this.heartbeatIntervalMs = 30000,
  this.longpollerTimeout = 20000,
  TimerCalculation? reconnectAfterMs,
  this.logger,
  this.params = const {},
  Map<String, String>? headers,
})  : endPoint = '$endPoint/${Transports.websocket}',
      headers = {
        ...Constants.defaultHeaders,
        if (headers != null) ...headers,
      },
      transport = transport ?? createWebSocketClient {
  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(callback: () => connect())},
    this.reconnectAfterMs,
  );
}