Azure Web PubSub service, as its name says, it is based on publish-subscribe pattern and enables us to build real-time web applications.
Some of the popular examples where we can use this service is, for any chat-based applications, any collaboration application, like white boarding application. We can also use this service for any application which needs instant push notifications. In fact, there are many more example, we can think about.
The best part is, we can use Azure Web PubSub service on all the platforms which supports WebSocket APIs and it allows up to 100 thousand concurrent connections at any point of time.
Components
required to create a basic chat application:
- Instance of Azure Web PubSub Service
- Publisher application
- Subscriber application
To know about how to create and use these components, I’ve created a complete video demonstrating these on my YouTube channel named Shweta Lodha.
C#
Code for Publisher and Subscriber:
Below is the C# code for the respective classes.
Publisher.cs
- var connectionString = "Your_ConnectionString_Here";
- var hub = "Your_Hub_Here";
- var serviceClient = new WebPubSubServiceClient(connectionString, hub);
- while (true)
- {
- Console.Write("Enter message: ");
- string message = Console.ReadLine();
- serviceClient.SendToAll(message);
- }
Subscriber.cs
- var connectionString = "Your_ConnectionString_Here";
- var hub = "Your_Hub_Here";
- // Either generate the URL or fetch it from server or fetch a temp one from the portal
- var serviceClient = new WebPubSubServiceClient(connectionString, hub);
- var url = serviceClient.GetClientAccessUri();
- using (var client = new WebsocketClient(url))
- {
- client.MessageReceived.Subscribe(msg => Console.WriteLine($"Message received: {msg}"));
- await client.Start();
- Console.WriteLine("I'm connected.");
- Console.Read();
- }
Hope you enjoyed learning about Azure Web PubSub service.
Comments
Post a Comment