initializeFanOutExchange method

Future<void> initializeFanOutExchange(
  1. String exchangeName,
  2. String? consumerTag
)

This provides a consumer-queue oriented fan-out model.

An exchange and a consumer queue is created and bound together. Multiple consumers can add and bind their queues to this exchange.

The BrokerApiClient will publish messages to the exchange and every message will be delivered to every consumer.

If no consumers are running, messages will be lost. To "rescue" those messages an "alternate exchange" can be used.

Queues are created with durable=false, autoDelete=true

Implementation

Future<void> initializeFanOutExchange(
    String exchangeName, String? consumerTag) async {
  final channel = await _brokerService.openChannel();
  _channels.add(channel);
  final exchange = await channel.exchange(exchangeName, ExchangeType.FANOUT);
  final queue = await channel.queue('', autoDelete: true);
  await queue.bind(exchange, '');
  final consumer =
      await queue.consume(consumerTag: consumerTag, noAck: false);
  consumer.listen(_onData);
}