publish method

Future<Publication> publish(
  1. String channel,
  2. dynamic data
)

Publish a message to a channel

Implementation

Future<Publication> publish(String channel, dynamic data) async {
  _logger.info('Client: Publishing to channel: $channel');
  _logger.info('Client: Current state: $_state');
  _logger.info('Client: Data: $data');

  if (_state != connected) {
    _logger.severe('Client: Cannot publish - not connected. State: $_state');
    throw FayeError.network('Not connected');
  }

  // Start connect cycle if this is the first operation after handshake
  _startConnectCycle();

  if (!channel.isValidChannel) {
    _logger.severe('Client: Invalid channel name: $channel');
    throw FayeError.channel(channel, 'Invalid channel name');
  }

  final channelObj = Channel(channel);
  final publicationId = _uuid.v4();

  _logger.info('Client: Calling dispatcher.publish()...');

  try {
    final response = await _dispatcher.publish(channel, data);
    _logger.info('Client: Dispatcher.publish() response: $response');

    final publication = Publication(
      id: publicationId,
      channel: channelObj,
      data: data,
    );

    final responseMessage = extractBayeuxMessage(response);

    if (responseMessage['successful'] == true) {
      publication.markSuccessful();
      _logger.info('Client: Published to $channel successfully');
    } else {
      final error = FayeError.fromBayeux(responseMessage['error'] ?? {});
      publication.markFailed(error);
      _logger.severe('Client: Publication to $channel failed: $error');
    }

    return publication;
  } catch (e) {
    _logger
        .severe('Client: Publication to $channel failed with exception: $e');

    final publication = Publication(
      id: publicationId,
      channel: channelObj,
      data: data,
    );

    final error = FayeError.network('Publication failed: $e');
    publication.markFailed(error);

    return publication;
  }
}