RealtimeSubscription constructor

RealtimeSubscription(
  1. String topic,
  2. RealtimeClient socket, {
  3. Map<String, dynamic> params = const {},
})

Implementation

RealtimeSubscription(this.topic, this.socket, {this.params = const {}})
    : _timeout = socket.timeout {
  _joinPush = Push(this, ChannelEvents.join, params, _timeout);
  _rejoinTimer =
      RetryTimer(() => rejoinUntilConnected(), socket.reconnectAfterMs);
  _joinPush.receive('ok', (response) {
    _state = ChannelStates.joined;
    _rejoinTimer.reset();
    for (final pushEvent in _pushBuffer) {
      pushEvent.send();
    }
    _pushBuffer = [];
  });

  onClose(() {
    _rejoinTimer.reset();
    socket.log('channel', 'close $topic ${joinRef()}');
    _state = ChannelStates.closed;
    socket.remove(this);
  });

  onError((String? reason) {
    if (isLeaving() || isClosed()) {
      return;
    }
    socket.log('channel', 'error $topic', reason);
    _state = ChannelStates.errored;
    _rejoinTimer.scheduleTimeout();
  });

  _joinPush.receive('timeout', (response) {
    if (!isJoining()) {
      return;
    }
    socket.log('channel', 'timeout $topic', _joinPush.timeout);
    _state = ChannelStates.errored;
    _rejoinTimer.scheduleTimeout();
  });

  on(
    ChannelEvents.reply.eventName(),
    (payload, {ref}) => trigger(
      replyEventName(ref),
      payload: payload,
    ),
  );
}