unsubscribe method

Future<String> unsubscribe([
  1. Duration? timeout
])

Leaves the channel

Unsubscribes from server events, and instructs channel to terminate on server. Triggers onClose() hooks.

To receive leave acknowledgements, use the a receive hook to bind to the server ack,

channel.unsubscribe().receive("ok", (_){print("left!");} );

Implementation

Future<String> unsubscribe([Duration? timeout]) {
  _state = ChannelStates.leaving;
  void onClose() {
    socket.log('channel', 'leave $topic');
    trigger(ChannelEvents.close.eventName(), 'leave', joinRef);
  }

  // Destroy joinPush to avoid connection timeouts during unscription phase
  joinPush.destroy();

  final completer = Completer<String>();

  final leavePush = Push(this, ChannelEvents.leave, {}, timeout ?? _timeout);

  leavePush.receive('ok', (_) {
    onClose();
    if (!completer.isCompleted) {
      completer.complete('ok');
    }
  }).receive('timeout', (_) {
    if (!completer.isCompleted) {
      onClose();
    }
    completer.complete('timed out');
  }).receive('error', (_) {
    onClose();
    if (!completer.isCompleted) {
      completer.complete('error');
    }
  });

  leavePush.send();

  if (!canPush) {
    leavePush.trigger('ok', {});
  }

  return completer.future;
}