# How to publish and subscribe messages with Azure Web PubSub

# Realtime Websocket connections with Azure Web PubSub

Sending and receiving real-time messages in your applications can be really useful. For instance in a chat application, or in a map application that updates points on a map. Building the infrastructure for real-time messaging is complicated and involves a lot of plumbing. That is where Azure Web PubSub (opens new window) comes in.

Azure Web PubSub is a serverless resource, that connects many clients over WebSocket connections. This provides real-time communication between clients, without the need to poll for messages or to set up complicated plumbing. Azure Web PubSub can scale to support millions of client connections, and can filter clients to only send messages to specific clients. You can use Azure Web PubSub with all sorts of programming languages, including C#, JavaScript, Python and Java.

In this post, we'll set up an Azure Web PubSub service (opens new window) and use it to send and receive messages using console applications.

# Prerequisites

If you want to follow along, you'll need the following:

# Publish and subscribe messages with Azure Web PubSub

We are going to send messages from a console application and receive them using another console application. Azure Web PubSub will provide the WebSocket connection that delivers the messages. We'll start by creating an Azure Web PubSub service:

  1. Go to the Azure portal (opens new window)
  2. Click the Create a resource button (the plus-sign in the top left corner)
  3. Search for web pubsub, select the "Web PubSub" result and click Create
    1. Select a Resource Group
    2. Type in a Name
    3. Select a Pricing tier. The free tier is fine for this exercise
    4. Click Review + create and then Create

(Create an Azure Web PubSub service in the Azure portal)

When the Azure Web PubSub service is created, navigate to it in the Azure portal. Navigate to the Keys blade and copy the Connection String. We need this later to connect the sample applications to the Azure Web PubSub service.

(Keys and connection string of the Azure Web PubSub service)

Next, we'll use the sample applications to send and receive messages using Azure Web PubSub. We use the sample console applications that you can find in this GitHub repo (opens new window).

  1. Download the sample applications from GitHub (opens new window). Inspect the program.cs files of the Subscriber and Publisher applications
  2. Open the Subscriber folder in a command window
  3. Execute the following command to start the subscriber console app, and connect it to Azure Web PubSub. You should see a "connected" message
dotnet run <Azure Web PubSub connection string> <hub name. You can make this up yourself>
1
  1. Open the Publisher folder in another command window
  2. Execute the following command to connect to Azure Web PubSub and send a message
dotnet run <Azure Web PubSub connection string> <hub name. You can make this up yourself> <message>
1

You'll see the message appear in the command window of the Subscriber application. You can repeat the publisher command to send more messages

(Messages received via Azure Web PubSub)

# Azure Web PubSub vs. Azure SignalR

On the surface, the capabilities of Azure Web PubSub (opens new window) seem very similar to those of Azure SignalR Service (opens new window). These services are different from each other, and support different scenarios. The main differences are:

Azure Web PubSub:

  • Only WebSockets
  • No automatic reconnect
  • No server-sent events, and long polling fallback, in case the WebSockets connection fails

Azure SignalR Service:

  • Handles connectivity and keep alive pings through the SignalR protocol
  • Supports RPC (Remote Procedure Calls), streaming, and more
  • Uses server-sent events, and long polling fallback, when a WebSocket connection isn't possible

Azure Web PubSub provides more reach through WebSockets than SignalR, but puts more management tasks in your hands, like client reconnection. So if you need more capabilities out of the box, use Azure SignalR. If you don’t, use Azure Web PubSub.

# Conclusion

Azure Web PubSub (opens new window) is an accessible real-time message service that you can use with any programming language that supports WebSocket connections. Go and check it out!