WebSocket Clients connecting to the service

Clients connect to the Web PubSub service (in below sections we refer it as the service) using WebSocket protocol. So languages having WebSocket client support can be used to write a client for the service. In below sections we show several WebSocket client samples in different languages.


The Web PubSub service uses JWT token to validate and auth the clients. Clients can either put the token in the access_token query parameter, or put it in Authorization header when connecting to the service.

A typical workflow is the client communicates with its app server first to get the URL of the service and the token. And then the client opens the WebSocket connection to the service using the URL and token it receives.

The portal also provides a dynamically generated Client URL with token for clients to start a quick test: Get URL)

NOTE Make sure to include neccessory roles when generating the token.

Client Role

To simplify the sample workflow, in below sections, we use this temporarily generated URL from portal for the client to connect, using <Client_URL_From_Portal> to represent the value.



Simple WebSocket Client

import asyncio
import websockets

async def hello():
    uri = '<Client_URL_From_Portal>'
    async with websockets.connect(uri) as ws:
        while True:
            await ws.send('hello')
            greeting = await ws.recv()


PubSub WebSocket Client

import asyncio
import websockets

async def join_group():
    uri = '<Client_URL_From_Portal>'
    async with websockets.connect(uri, subprotocols=['json.webpubsub.azure.v1']) as ws:
        await ws.send('{"type":"joinGroup","ackId":1,"group":"group1"}')
        return await ws.recv()


Find an issue? Please report it here. Your feedback is highly appreciated! Provide it via Azure Feedback Forum.