Exercise - Publish and subscribe to events using Pub/Sub
In your retail application, the software built by the inventory and shipping departments needs to respond to each other’s events as part of a typical workflow. For example, the inventory microservice needs to deduct the item from its inventory once an order is shipped.
In this exercise, implement Pub/Sub in an Azure Cache for Redis instance using multiple console windows.
Open the Azure Cache for Redis console in two browser instances
Observe two separate clients sending and receiving messages by using two unique browser instances. Each browser instance has the Redis console to help illustrate real-time Pub/Sub functionality.
Open the first browser instance
Sign in to the Azure portal using the same account you used to activate the sandbox.
Within the Azure services category, select More services, select the Databases category and then select Azure Cache for Redis.
Select the Azure Cache for Redis instance you created in a previous exercise.
In the resource pane, select Console to open the Redis console.
Leave the console open to complete subsequent tasks in this exercise.
Open the second browser instance
Sign in to the Azure portal using the same account you used to activate the sandbox and a separate browser window or tab.
Within the Azure services category, select More services, select the Databases category and then select Azure Cache for Redis.
Select the Azure Cache for Redis instance you created in a previous exercise.
In the resource pane, select Console to open the Redis console.
Leave the console open to complete subsequent tasks in this exercise.
At this point, you should have two browser instances open each with an instance of the Redis console.
Tip
If your operating system supports it, we recommend docking the browser windows side-by-side to simplify the remainder of this exercise.
Subscribe and publish messages to a known channel
Subscribe to channels using the SUBSCRIBE
command and then publish messages using the PUBLISH
command.
In the console of the first browser instance, perform the following actions:
Enter the following command and use the ENTER key to begin listening for messages on the org.shipping.alerts channel.
SUBSCRIBE org.shipping.alerts
Observe the response from the console indicating that it's now listening on the org.shipping.alerts channel.
Reading messages... (press ENTER to quit) 1) "subscribe" 2) "org.shipping.alerts" 3) (integer) 1
In the console of the second browser instance, perform the following actions:
Enter the following command and use the ENTER key to send a new message with the content labelprint-sdf9878 to the org.shipping.alerts channel.
PUBLISH org.shipping.alerts labelprint-sdf9878
Enter the following command and use the ENTER key to send a new message with the content labelprint-sdf9878 to the org.shipping.alerts channel.
PUBLISH org.shipping.alerts packagesent-sdf9878
Back in the console of the first browser instance, perform the following actions:
Observe the first response from the console indicating that it has received a new message on the org.shipping.alerts channel.
1) "message" 2) "org.shipping.alerts" 3) "labelprint-sdf9878"
Observe the second response from the console indicating that it has received a new message on the org.shipping.alerts channel.
1) "message" 2) "org.shipping.alerts" 3) "packagesent-sdf9878"
Use the ENTER key to stop the console from listening to events.
Enter the following command and use the ENTER key to clear the console output.
clear
Back in the console of the second browser instance, perform the following actions:
Enter the following command and use the ENTER key to clear the console output.
clear
Subscribe to a channel pattern and listen for messages
Subscribe to a pattern of channels using the PSUBSCRIBE
command and then publish a message using the PUBLISH
command.
In the console of the first browser instance, perform the following actions:
Enter the following command and use the ENTER key to begin listening for messages on the org.shipping.alerts channel.
PSUBSCRIBE org.inventory.*
Observe the response from the console indicating that it's now listening on the org.inventory.* channel pattern.
Reading messages... (press ENTER to quit) 1) "psubscribe" 2) "org.inventory.*" 3) (integer) 1
In the console of the second browser instance, perform the following actions:
Enter the following command and use the ENTER key to send a new message with the content item-sku-318947 to the org.inventory.empty channel.
PUBLISH org.inventory.empty item-sku-318947
Enter the following command and use the ENTER key to send a new message with the content order-dsy3821 to the org.shipping.sent channel.
PUBLISH org.shipping.sent order-dsy3821
Note
Since this channel does not match the org.inventory.* pattern, you shouldn't expect this command to send a message that your other client will receive.
Back in the console of the first browser instance, perform the following actions:
Need help? See our troubleshooting guide or provide specific feedback by reporting an issue.