Routing keys are one of the most powerful features of RabbitMQ. They allow the message broker to flexibly route messages to queues based on pattern matching, rather than exact matches.
Routing keys in RabbitMQ are made up of words separated by dots. For example, the routing key user.created is made up of two words: user and created. The routing key peril.game.won is made up of three words: peril, game, and won.
RabbitMQ supports two types of wildcards in routing keys:
* (star) substitutes for exactly one word# (hash) substitutes for zero or more wordsA queue bound to the key peril.# will pick up messages published to routing keys:
peril.game.wonperil.game.lostperil.playerperilIt will not match:
game.wonperilgame.wonA queue bound to the key peril.*.won will pick up messages published to routing keys:
peril.game.wonperil.player.wonIt will not match:
peril.game.lostperil.wonWhenever a player (client) uses the move command in our "Peril" game, we want to broadcast the move to all other connected players. We'll publish the message to the army_moves.username routing key, where username is the name of the player who made the move.
Each client needs to bind a queue to the exchange using the routing key army_moves.* so that they get all the moves from other players.
Test the changes by running 2 or 3 clients. Have one of the clients spawn a couple of units:
spawn americas infantry
spawn antarctica cavalry
Then move the units:
move asia 1 2
All clients, including the one who made the move, should log a message that they successfully detected the move.
Run and submit the CLI tests with the clients still running (and therefore their queues and bindings still in existence).