Quickstart: Publish messages using the Azure Web PubSub service SDK
Article
Azure Web PubSub helps you manage WebSocket clients. This quickstart shows you how to publish messages to WebSocket clients using Azure Web PubSub service SDK.
Prerequisites
An Azure subscription, if you don't have one, create a free account.
a Bash and PowerShell command shell. The Python, JavaScript and Java samples require a Bash command shell.
To sign in to Azure from the CLI, run the following command and follow the prompts to complete the authentication process. If you're using Cloud Shell, it isn't necessary to sign in.
az login
Ensure you're running the latest version of the CLI via the upgrade command.
az upgrade
Next, install or update the Azure Web PubSub extension for the CLI if it wasn't installed with az upgrade.
az extension add --name webpubsub --upgrade
1. Create a resource group
Set the following environment variables. Replace the <placeholder> with a unique Web PubSub name.
az webpubsub key show --name $WEB_PUBSUB_NAME --resource-group $RESOURCE_GROUP --query primaryConnectionString
az webpubsub key show --name $WebPubSubName --resource-group $ResourceGroupName --query primaryConnectionString
3. Connect a client to the service instance
Create a Web PubSub client. The client maintains a connection to the service until it's terminated.
Use the az webpubsub client command to start a WebSocket client connection to the service. The clients always connect to a hub, so provide a hub name for the client to connect to.
The connection to the Web PubSub service is established when you see a JSON message indicating that the client is now successfully connected, and is assigned a unique connectionId:
You'll use the Azure Web PubSub SDK to publish a message to all the clients connected to the hub.
You can choose between C#, JavaScript, Python and Java. The dependencies for each language are installed in the steps for that language. Python, JavaScript and Java require a bash shell to run the commands in this quickstart.
Set up the project to publish messages
Open a new command shell for this project.
Save the connection string from the client shell. Replace the <your_connection_string> placeholder with the connection string you displayed in an earlier step.
Add a new project named publisher and the SDK package Azure.Messaging.WebPubSub.
mkdir publisher
cd publisher
dotnet new console
dotnet add package Azure.Messaging.WebPubSub
Update the Program.cs file to use the WebPubSubServiceClient class to send messages to the clients. Replace the code in the Program.cs file with the following code.
using System;
using System.Threading.Tasks;
using Azure.Messaging.WebPubSub;
namespace publisher
{
class Program
{
static async Task Main(string[] args)
{
if (args.Length != 3) {
Console.WriteLine("Usage: publisher <connectionString> <hub> <message>");
return;
}
var connectionString = args[0];
var hub = args[1];
var message = args[2];
var service = new WebPubSubServiceClient(connectionString, hub);
// Send messages to all the connected clients
// You can also try SendToConnectionAsync to send messages to the specific connection
await service.SendToAllAsync(message);
}
}
}
The service.SendToAllAsync() call simply sends a message to all connected clients in the hub.
Run the following command to publish a message to the service.
dotnet run $connection_string "myHub1" "Hello World"
The previous command shell containing the Web PubSub client shows the received message.
Go to the src/main/java/com/webpubsub/quickstart directory.
Replace the contents in the App.java file with the following code:
package com.webpubsub.quickstart;
import com.azure.messaging.webpubsub.*;
import com.azure.messaging.webpubsub.models.*;
/**
* Publish messages using Azure Web PubSub service SDK
*
*/
public class App
{
public static void main( String[] args )
{
if (args.length != 3) {
System.out.println("Expecting 3 arguments: <connection-string> <hub-name> <message>");
return;
}
WebPubSubServiceClient service = new WebPubSubServiceClientBuilder()
.connectionString(args[0])
.hub(args[1])
.buildClient();
service.sendToAll(args[2], WebPubSubContentType.TEXT_PLAIN);
}
}
This code uses the Azure Web PubSub SDK to publish a message to the service. The service.sendToAll() call sends a message to all connected clients in a hub.
Return t the webpubsub-quickstart-publisher directory containing the pom.xml file and compile the project by using the following mvn command.
mvn compile
Build the package.
mvn package
Run the following mvn command to execute the app to publish a message to the service:
This quickstart provides you a basic idea of how to connect to the Web PubSub service and how to publish messages to the connected clients.
In real-world applications, you can use SDKs in various languages build your own application. We also provide Function extensions for you to build serverless applications easily.
Use these resources to start building your own application: