Skip to main content

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

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>'>

Examples

Sending a request

>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> from azure.core.credentials import AzureKeyCredential
>>> from azure.messaging.webpubsubservice.rest 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 
202
>>> 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

Hub

Hub is a logical set of connections. All connections to Web PubSub connect to a specific hub. Messages that are broadcast to the hub are dispatched to all connections to that hub. For example, 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.

Connection

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

Message

A message is either a UTF-8 encoded string, json or raw binary data.

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 logging

logging.basicConfig(level=logging.DEBUG)

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

azure-messaging-webpubsubservice-1.0.0b1.zip (44.0 kB view details)

Uploaded Source

Built Distribution

azure_messaging_webpubsubservice-1.0.0b1-py2.py3-none-any.whl (30.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file azure-messaging-webpubsubservice-1.0.0b1.zip.

File metadata

  • Download URL: azure-messaging-webpubsubservice-1.0.0b1.zip
  • Upload date:
  • Size: 44.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for azure-messaging-webpubsubservice-1.0.0b1.zip
Algorithm Hash digest
SHA256 7f609a52a2a448bbefc52fc1be2fb2b038ebc2154c6c215cd3ce3e4e8dfd858f
MD5 255315d093ed81a6ae022a6b4c504c38
BLAKE2b-256 9d0da5212baf93457ba8edece6acb210b39d5aecc596742d5711e6384b69a5cf

See more details on using hashes here.

File details

Details for the file azure_messaging_webpubsubservice-1.0.0b1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for azure_messaging_webpubsubservice-1.0.0b1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b0de2b67293f2169bd2f3a8da5e54ed1e6f4f95997b23bdf9e51fc52dcf79f9b
MD5 d6f4cfbc3334cf3428b49c876f8dd8fe
BLAKE2b-256 c2fd4f87bf14713a55d8cf342e26f783c60f4f0fe254480371e41924cb69d96b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page