Quickstart: Connect to the Azure Web PubSub instance from CLI
This quickstart shows you how to connect to the Azure Web PubSub instance and publish messages to the connected clients using the Azure CLI.
If you don't have an Azure subscription, create an Azure free account before you begin.
Prerequisites
Use the Bash environment in Azure Cloud Shell. For more information, see Quickstart for Bash in Azure Cloud Shell.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
- This quickstart requires version 2.22.0 or higher of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed.
Create a resource group
A resource group is a logical container into which Azure resources are deployed and managed. Use the az group create command to create a resource group named myResourceGroup
in the eastus
location.
az group create --name myResourceGroup --location EastUS
Create a Web PubSub instance
Run az extension add to install or upgrade the webpubsub extension to the current version.
az extension add --upgrade --name webpubsub
Use the Azure CLI az webpubsub create command to create a Web PubSub in the resource group you've created. The following command creates a Free Web PubSub resource under resource group myResourceGroup in EastUS:
Important
Each Web PubSub resource must have a unique name. Replace <your-unique-resource-name> with the name of your Web PubSub in the following examples.
az webpubsub create --name "<your-unique-resource-name>" --resource-group "myResourceGroup" --location "EastUS" --sku Free_F1
The output of this command shows properties of the newly created resource. Take note of the two properties listed below:
- Resource Name: The name you provided to the
--name
parameter above. - hostName: In the example, the host name is
<your-unique-resource-name>.webpubsub.azure.com/
.
At this point, your Azure account is the only one authorized to perform any operations on this new resource.
Play with the instance
Connect to the service
Use the Azure CLI az webpubsub client command to start a WebSocket client connection to the service created from the previous step, providing the following information:
- Hub name: A string of 1 to 127 characters. It should start with alphabetic characters
(a-z, A-Z)
and only contain alpha-numeric(0-9, a-z, A-Z)
characters or underscore(_)
.
Hub is a logical set of the connected WebSocket connections. Check About Hubs, groups and connections for details about the concepts.
Important
Replace <your-unique-resource-name> with the name of your Web PubSub resource created from the previous steps.
- Hub name: myHub1.
- Resource group name: myResourceGroup.
- User ID: user1
az webpubsub client start --name "<your-unique-resource-name>" --resource-group "myResourceGroup" --hub-name "myHub1" --user-id "user1"
You can see that the command established a WebSocket connection to the Web PubSub service and you received a JSON message indicating that it is now successfully connected, and is assigned with a unique connectionId
:
{"type":"system","event":"connected","userId":"user1","connectionId":"<your_unique_connection_id>"}
Play with it and try joining to groups using joingroup <group-name>
and send messages to groups using sendtogroup <group-name>
:
joingroup group1
sendtogroup group1 hello
Publish messages and manage the clients
Azure CLI also provides az webpubsub service commands to manage the client connections.
Open another CLI command, and you can broadcast messages to the clients:
- Hub name: myHub1.
- Resource group name: myResourceGroup.
az webpubsub service broadcast --name "<your-unique-resource-name>" --resource-group "myResourceGroup" --hub-name "myHub1" --payload "Hello World"
Switch back to the previous CLI command and you can see that the client received message:
{"type":"message","from":"server","dataType":"text","data":"Hello World"}
You can also list all the available commands using --help
option and play with the listed commands.
az webpubsub service --help
Next steps
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: