This post lets us create a publisher and subscriber messaging app using Azure Web PubSub Service and C#. In the previous post, Getting Started with Azure Web PubSub Service; we have learned how to create Azure Web PubSub Service in the Azure Portal and tested the messaging using one sample application. This post will use the Azure SDK client library for the Web PubSub service to build a simple messaging application using C#.
Getting Started with Azure Web PubSub Service
Messaging App using Azure Web PubSub Service
To start with, create a Blank Solution in Visual Studio, and add two console projects “AzureWebPubSubDemoPublisher” and “AzureWebPubSubDemoSubscriber“. Once you are done with that, your solution structure will look like the following.
Create the Publisher
Firstly, let us start create the publisher App. Add “Azure.Messaging.WebPubSub” NuGet package to the solution.
Once the package is added, the solution structure will looks like follows.
Replace the program.cs code with the following code block and update the Keys and Hub Name generated from the portal.
using Azure.Messaging.WebPubSub;
using System;
var webPubSubServiceClient = new WebPubSubServiceClient("Endpoint=https://azurepubsubdemo.webpubsub.azure.com;AccessKey=tGXgGGnOSpnCSiVdZIZ+P1L19/YWBjcqNjEf+xEgD/0=;Version=1.0;", "Hub");
string send = "y";
while (send.ToLower().Equals("y"))
{
Console.WriteLine("Sending Message..");
webPubSubServiceClient.SendToAll("This is a message from your publisher");
Console.WriteLine("Message Sent");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Send another message? Y/N");
send = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
}
The above code block used C# 9.0 Top-Level Statement feature: Top-Level Statement allows developers to write programs without explicitly defining the class or main method. Until C# 9.0, It was all about the Main() method where program control start and ends. With C# 9.0, you don’t need to mention the Main() method or Class definition explicitly using Top-Level Statement.
In case you are not using C# 9.0, you have the same code block written with the Main().
Top Level Statement in C# 9.0
Get the PubSub Keys from Portal
We can connect with the application either using the Connection String or from the Client URL Generator. Here we have used the primary connecting string.
With that, we are done with the publisher part.
In the next section we will create the subscribers to consume the messages.
Create the Subscriber
In the subscriber project, we need to add two package, “Azure.Messaging.WebPubSub” and “WebSocket.Client” .
Open the program.cs file, and update the following code. That’s should be enough from here.
using System;
using Azure.Messaging.WebPubSub;
using Websocket.Client;
var webPubSubServiceClient = new WebPubSubServiceClient("Endpoint=https://azurepubsubdemo.webpubsub.azure.com;AccessKey=tGXgssdsdsdsdD/0=;Version=1.0;", "Hub");
var url = webPubSubServiceClient.GetClientAccessUri();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Connecting with Client....");
using (var websocketClient = new WebsocketClient(url))
{
Console.ForegroundColor = ConsoleColor.Green;
websocketClient.MessageReceived.Subscribe(receivedMessage => Console.WriteLine($"Message received: {receivedMessage}"));
await websocketClient.Start();
Console.WriteLine("Connected.. Waiting for Message");
Console.Read();
}
Test the Azure Web PubSub Message App
Let us know test both the application together and see how it works. Run both the applications as “Debug – Start New Instance” so that you can run both the applications together.
Alternatively, you can setup multiple project as startup as well. check this out
Finally, when the both the console app is running, you can just sent out a message the publisher, and see the message is getting reflected to subscriber as well.
You can also run multiple subscriber, and you will find all of them are receiving message as we have used “SendAll()“
To summarize, in this post we have used one of the existing Azure Web PubSub services and created one simple message application using C#
Pingback: Dew Drop – May 7, 2021 (#3438) – Morning Dew by Alvin Ashcraft