Azure Web PubSub is a fully managed service from Microsoft Azure. Azure Web PubSub supports fully native and serverless WebSockets to build a cloud solution that required real-time communication. Firstly, Publisher and subscriber patterns are one of the most common business needs for any large-scale transaction system. moreover, Be it Messaging Application, Live Sharing, IoT based solution, real-time order management required loosely coupled architecture; which can be easily achieved by publisher-subscriber pattern. Azure Web Pubsub automatically provision the web socket server when we create the new instance. It also supports auto-scale, load balancing, large-scale client connections, and smooth integration between different platforms without focusing on infrastructure. In Getting started with Azure Web PubSub post, Let us explore how to create Azure Web PubSub and Develop with Azure PubSub Platform.
Getting started with Azure Web PubSub
In this post, let us try to learn and understand following
- Create Azure Web PubSub Service
- Explore Azure Portal Settings for PubSub Service
- Test Your Newly Created Service
- Create a Custom Publisher and Test with Azure Web PubSub Platform
Create Azure Web PubSub Service
Firstly, To create Azure Web PubSub Service instance, From the Azure Portal, Search for “Web PubSub Service” and click on “Create“
Secondly, Provide all the necessary information to create the instance including name, region, SKU, and Unit. For the demonstration purpose let us consider “Free” Pricing Ter, which only supports 1 Unit. Finally, Click on “Review + Create“
However, Here is a quick comparison between two different SKUs available for the Azure PubSub Service. For instance, If we move from Free Tier to Standard Titer, it provides high scalability and large-scale communication. Above all, As this service currently under public preview, no SLA is provided.
After service is created successfully, to start using the service, select the “Keys” available under “Settings.” This section will get all the necessary details for any integration with our application.
We can connect with the application either using the Connection String or from the Client URL Generator. The hub creates the isolation layer between the connection by allowing messages within the same hub only. Once you refer to a common hub, no matter what are the different client applications, the connection will be isolated within the hub.
Test Your Service
In this section, let us try the connectivity and messaging using the Demo Web Site available from Microsoft Azure PubSub Service to Test out. Open the Demo Web URL,
In the Key Settings, Enable the “Send To Group” and “Join Leave Group” in the Client URL Generator. Copy the “Client Access URL.”
Then click on Connect after providing the Client Access URL. As a result, Once it gets connected successfully, you will find the message for Client Websocket Opened, and the red icon will be turned to green.
for instance, now you can create some random group and join them as subscribers, and from the publisher section, try publishing the message for a specific set of groups that you created. You should be able to view the message transmission between the group within the same hub,
You come back to the Azure Portal and view the metric to see the real-time data ingestion and connection for the Azure PubSub Service.
Create Azure Web PubSub – Custom Publisher using C#
Let us quickly enhance this by using a custom C# Publisher.
Create a simple C# Console Application, and Add the “Azure.Messaging.WebPubSub” NuGet package.
As next step, Write the following piece of code in the console.
using Azure.Messaging.WebPubSub; //replace with your connection string and hub name" var serviceClient = new WebPubSubServiceClient("connection string from the keys;", "Hub"); serviceClient.SendToAll("This is a message from your publisher ! For All Group"); serviceClient.SendToGroup("Group 1", " Group 1, this is message for you");
C# 9.0 Tip: Top Level Statement in C# 9.0
Now, run the application, and keep the demo website open where tested our previous example.
So, you can see now your subscriber is getting a message from a different custom publisher, who sent one message to all the groups, and on specific to group 1.
In conclusion, I hope this gives you a quick understanding of Azure PubSub Services and how we can start leveraging them. However, to keep in mind, This service is in public preview.
Read more about Azure Web PubSub from here