Is it possible to send one message to several exchangers?

45 views Asked by At

I just started studying rabbit and the following question arose: if I have a exchange TOPIC type with name adverts and a queues:

adverts_create (routing_key: `advert.create`)
adverts_edit (routing_key: `advert.edit`)

then can I send a message to several queues at once? Something like this:

$msg = new AMQPMessage('text');
$channel->basic_publish($msg, 'adverts', 'advert.*');
1

There are 1 answers

1
Arib On

Topic exchanges allow for more flexible routing of messages based on wildcard patterns in the routing key.

In your example, you have two queues (adverts_create and adverts_edit) associated with the topic exchange named adverts. The routing keys are advert.create and advert.edit.

If you want to send a message to multiple queues that match a certain pattern, you can indeed use wildcards in the routing key when publishing messages to the topic exchange. In RabbitMQ, the routing key follows a dot-separated hierarchy, and you can use asterisks (*) as wildcards to match any word in that position.

For example, your code snippet:

$msg = new AMQPMessage('text');
$channel->basic_publish($msg, 'adverts', 'advert.*');

This code would publish the message to any queue bound to the adverts exchange with a routing key that starts with advert. and has one more word following it. So, both advert.create and advert.edit queues would receive the message in this case.

Just make sure that the queues are correctly bound to the exchange with the appropriate routing key patterns.