Create a Messaging App using Azure Web PubSub Service and C#

Create a Messaging App using Azure Web PubSub Service and C#

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.

Azure Web PubSub Demo Project

Create the Publisher

Firstly, let us start create the publisher App. Add “Azure.Messaging.WebPubSub” NuGet package to the solution.

Create Azure Web PubSub  app with nuget package

Once the package is added, the solution structure will looks like follows.

Create Azure Web PubSub Publisher

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.

Get URL and Keys for Pubsub

With that, we are done with the publisher part.

Create Azure Web PubSub Publisher Output

In the next section we will create the subscribers to consume the messages.

10 Azure Cloud services that every Developers, Consultant, and Architects should Know and Learn it well

Create the Subscriber

In the subscriber project, we need to add two package, “Azure.Messaging.WebPubSub” and “WebSocket.Client” .

Create Azure Web PubSub Subscriber

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.

start new instance of project

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.

pubsub subscriber and publisher

You can also run multiple subscriber, and you will find all of them are receiving message as we have used “SendAll()

multiple pubsub subscriber and publisher

To summarize, in this post we have used one of the existing Azure Web PubSub services and created one simple message application using C#

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

One Comment to “Create a Messaging App using Azure Web PubSub Service and C#”

Comments are closed.