🔥 Checkout the Azure Developer page at azure.com/developer (opens new window).
💡 Learn more : Azure Web PubSub overview (opens new window).
📺 Watch the video : How to publish and subscribe messages with Azure Web PubSub (opens new window).
# 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.
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.
If you want to follow along, you'll need the following:
- An Azure subscription (If you don't have an Azure subscription, create a free account (opens new window) before you begin)
- .NET Core 2.1 or above (opens new window)
# 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:
- Go to the Azure portal (opens new window)
- Click the Create a resource button (the plus-sign in the top left corner)
- Search for web pubsub, select the "Web PubSub" result and click Create
- Select a Resource Group
- Type in a Name
- Select a Pricing tier. The free tier is fine for this exercise
- 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).
- Download the sample applications from GitHub (opens new window). Inspect the program.cs files of the Subscriber and Publisher applications
- Open the Subscriber folder in a command window
- 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>
- Open the Publisher folder in another command window
- 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>
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.
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!