Azure Web PubSub service client library for Python

Use the client library to:

  • Send messages to hubs and groups.
  • Send messages to particular users and connections.
  • Organize users and connections into groups.
  • Close connections
  • Grant/revoke/check permissions for an existing connection
Source code Package (Pypi) API reference documentation Product documentation

Getting started

Installating the package

python -m pip install azure-messaging-webpubsubservice


Authenticating the client

In order to interact with the Azure WebPubSub service, you’ll need to create an instance of the WebPubSubServiceClient class. In order to authenticate against the service, you need to pass in an AzureKeyCredential instance with endpoint and api key. The endpoint and api key can be found on the azure portal.

>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> from azure.core.credentials import AzureKeyCredential
>>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=AzureKeyCredential('somesecret'))
>>> client
<WebPubSubServiceClient endpoint:'<endpoint>'>


Sending a request

>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> from azure.core.credentials import AzureKeyCredential
>>> from import build_send_to_all_request
>>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=AzureKeyCredential('somesecret'))
>>> request = build_send_to_all_request('default', json={ 'Hello':  'webpubsub!' })
>>> request
<HttpRequest [POST], url: '/api/hubs/default/:send?api-version=2020-10-01'>
>>> response = client.send_request(request)
>>> response
<RequestsTransportResponse: 202 Accepted>
>>> response.status_code 
>>> with open('file.json', 'r') as f:
>>>    request = build_send_to_all_request('ahub', content=f, content_type='application/json')
>>>    response = client.send_request(request)
>>> print(response)
<RequestsTransportResponse: 202 Accepted>

Key concepts


Connections, represented by a connection id, represent an individual websocket connection to the Web PubSub service. Connection id is always unique.


Hub is a logical concept for a set of connections. Connections are always connected to a specific hub. Messages that are broadcast to the hub are dispatched to all connections to that hub. Hub can be used for different applications, different applications can share one Azure Web PubSub service by using different hub names.


Group allow broadcast messages to a subset of connections to the hub. You can add and remove users and connections as needed. A client can join multiple groups, and a group can contain multiple clients.


Connections to Web PubSub can belong to one user. A user might have multiple connections, for example when a single user is connected across multiple devices or multiple browser tabs.


Using this library, you can send messages to the client connections. A message can either be string text, JSON or binary payload.



This SDK uses Python standard logging library. You can configure logging print out debugging information to the stdout or anywhere you want.

import logging


Http request and response details are printed to stdout with this logging config.

Next steps

Please take a look at the samples directory for detailed examples on how to use this library.

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