/** * Initialize event consumer. * You should call consumeEvents() next, to receive events. * * @param callable $callback Function to call with each consumed message */ public function initEventConsumer($callback) { if (!isset($this->eventChannel)) { $connection = $this->getConnection(); $ch = $connection->channel(); Util::declareQueue($connection, $this->eventQueue, ['durable' => true, 'exclusive' => false]); Util::declareExchange($connection, $this->eventExchange, 'direct', ['auto_delete' => false]); $ch->queue_bind($this->eventQueue, $this->eventExchange, $this->eventRoutingKey); $wrapper = function ($msg) use($callback) { $event = Event::newFromMessage($msg); return $callback($event); }; $ch->basic_consume($this->eventQueue, '', false, false, false, false, $wrapper); $this->eventChannel = $ch; } else { throw new Errors\ClientException("Event consumer has already been started"); } }