publishMessage method

Future<CtrlMessage?> publishMessage(
  1. Message message
)

Publish message created by Topic.createMessage.

Implementation

Future<CtrlMessage?> publishMessage(Message message) async {
  if (!isSubscribed) {
    return Future.error(Exception('Cannot publish on inactive topic'));
  }

  message.setStatus(message_status.SENDING);

  try {
    var response = await _tinodeService.publishMessage(message);
    CtrlMessage? ctrl;
    if (response is CtrlMessage) {
      ctrl = response;
    } else if (response is Map<String, dynamic>) {
      ctrl = CtrlMessage.fromMessage(response);
    }
    if (ctrl != null) {
      message.ts = ctrl.ts;
      var seq = ctrl.params['seq'];
      if (seq != null) {
        message.setStatus(message_status.SENT);
      }
      routeData(message.asDataMessage(_authService.userId ?? '', seq));
    }
    return ctrl;
  } catch (e) {
    _loggerService.warn('Message rejected by the server');
    _loggerService.warn(e.toString());
    message.setStatus(message_status.FAILED);
    onData.add(null);
    return Future.value(CtrlMessage());
  }
}