pub method

Future<bool> pub(
  1. String? subject,
  2. Uint8List data, {
  3. String? replyTo,
  4. bool? buffer,
  5. Header? header,
})

publish by byte (Uint8List) return true if sucess sending or buffering return false if not connect

Implementation

Future<bool> pub(String? subject, Uint8List data,
    {String? replyTo, bool? buffer, Header? header}) async {
  buffer ??= defaultPubBuffer;
  if (status != Status.connected) {
    if (buffer) {
      _pubBuffer.add(_Pub(subject, data, replyTo));
      return true;
    } else {
      return false;
    }
  }

  String cmd;
  var headerByte = header?.toBytes();
  if (header == null) {
    cmd = 'pub';
  } else {
    cmd = 'hpub';
  }
  cmd += ' $subject';
  if (replyTo != null) {
    cmd += ' $replyTo';
  }
  if (headerByte != null) {
    cmd += ' ${headerByte.length}  ${headerByte.length + data.length}';
    _add(cmd);
    var dataWithHeader = headerByte.toList();
    dataWithHeader.addAll(data.toList());
    _addByte(dataWithHeader);
  } else {
    cmd += ' ${data.length}';
    _add(cmd);
    _addByte(data);
  }

  if (_connectOption.verbose == true) {
    var ack = await _ackStream.stream.first;
    return ack;
  }
  return true;
}