Microsoft Azure WebPubSub Service Client Library for Python
Project description
Azure WebPubSubService client library for Python
Azure Web PubSub Service is a service that enables you to build real-time messaging web applications using WebSockets and the publish-subscribe pattern. Any platform supporting WebSocket APIs can connect to the service easily, e.g. web pages, mobile applications, edge devices, etc. The service manages the WebSocket connections for you and allows up to 100K concurrent connections. It provides powerful APIs for you to manage these clients and deliver real-time messages.
Any scenario that requires real-time publish-subscribe messaging between server and clients or among clients, can use Azure Web PubSub service. Traditional real-time features that often require polling from server or submitting HTTP requests, can also use Azure Web PubSub service.
We list some examples that are good to use Azure Web PubSub service:
- High frequency data updates: gaming, voting, polling, auction.
- Live dashboards and monitoring: company dashboard, financial market data, instant sales update, multi-player game leader board, and IoT monitoring.
- Cross-platform live chat: live chat room, chat bot, on-line customer support, real-time shopping assistant, messenger, in-game chat, and so on.
- Real-time location on map: logistic tracking, delivery status tracking, transportation status updates, GPS apps.
- Real-time targeted ads: personalized real-time push ads and offers, interactive ads.
- Collaborative apps: coauthoring, whiteboard apps and team meeting software.
- Push instant notifications: social network, email, game, travel alert.
- Real-time broadcasting: live audio/video broadcasting, live captioning, translating, events/news broadcasting.
- IoT and connected devices: real-time IoT metrics, remote control, real-time status, and location tracking.
- Automation: real-time trigger from upstream events.
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
Prequisites
- Python 2.7, or 3.6 or later is required to use this package.
- You need an Azure subscription, and a Azure WebPubSub service instance to use this package.
- An existing Azure Web PubSub service instance.
Authenticating the client
1. Create the client from the service connection string
You can get the API key or Connection string in the Azure Portal. Once you have the value for the API key, you can pass it as a string into an instance of AzureKeyCredential. Use the key as the credential parameter to authenticate the client:
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> from azure.core.credentials import AzureKeyCredential
>>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=AzureKeyCredential("<api_key>"))
Once you have the value for the connection string, you can pass it as a string into the function from_connection_string
and it will
authenticate the client:
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> client = WebPubSubServiceClient.from_connection_string(connection_string='<connection_string>')
2. Create with an Azure Active Directory Credential
To use an Azure Active Directory (AAD) token credential, provide an instance of the desired credential type obtained from the azure-identity library.
To authenticate with AAD, you must first pip install azure-identity
and
enable AAD authentication on your Webpubsub resource
After setup, you can choose which type of credential from azure.identity to use. As an example, DefaultAzureCredential can be used to authenticate the client:
Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
Use the returned token credential to authenticate the client:
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> from azure.identity import DefaultAzureCredential
>>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=DefaultAzureCredential())
Examples
Broadcast messages
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> from azure.identity import DefaultAzureCredential
>>> from azure.core.exceptions import HttpResponseError
>>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=DefaultAzureCredential())
>>> with open('file.json', 'r') as f:
try:
client.send_to_all('ahub', content=f, content_type='application/json')
except HttpResponseError as e:
print('service responds error: {}'.format(e.response.json()))
Key concepts
Connection
Connections, represented by a connection id, represent an individual websocket connection to the Web PubSub service. Connection id is always unique.
Hub
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
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.
User
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.
Message
Using this library, you can send messages to the client connections. A message can either be string text, JSON or binary payload.
Troubleshooting
Logging
This SDK uses Python standard logging library. You can configure logging print out debugging information to the stdout or anywhere you want.
import sys
import logging
from azure.identity import DefaultAzureCredential
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
# Create a logger for the 'azure' SDK
logger = logging.getLogger('azure')
logger.setLevel(logging.DEBUG)
# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)
endpoint = "<endpoint>"
credential = DefaultAzureCredential()
# This client will log detailed information about its HTTP sessions, at DEBUG level
client = WebPubSubServiceClient(endpoint=endpoint, credential=credential, logging_enable=True)
Similarly, logging_enable
can enable detailed logging for a single call,
even when it isn't enabled for the client:
result = client.send_to_all(..., logging_enable=True)
Http request and response details are printed to stdout with this logging config.
Next steps
More examples are coming soon...
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file azure-messaging-webpubsubservice-1.0.0b2.zip
.
File metadata
- Download URL: azure-messaging-webpubsubservice-1.0.0b2.zip
- Upload date:
- Size: 52.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a46d692d3ffd1550e6b1cdacf72315f555a2441da120dda2d9b6d0a07f189b0e |
|
MD5 | 079bac8a17e1749e80358928a593b669 |
|
BLAKE2b-256 | 390f17e27d95f5f61e20ed5c4203e121784b11000ddaa4ad3a9ae9ce6be7bbff |
File details
Details for the file azure_messaging_webpubsubservice-1.0.0b2-py2.py3-none-any.whl
.
File metadata
- Download URL: azure_messaging_webpubsubservice-1.0.0b2-py2.py3-none-any.whl
- Upload date:
- Size: 29.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64cf6df32b17fd9eb766c06da9ab03a857aac6c02bd6ffe77edb8fe1d75d8a6a |
|
MD5 | 2805202e80ec149d2400ef71b630aba5 |
|
BLAKE2b-256 | e0bc3b7237e82ad2972f3f67a1703cecf82e0fa93144cd8f49ad1b0c2efefaac |