.. role:: raw-html-m2r(raw) :format: html 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 ^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: bash 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 ^^^^^^^^^^^^^^^^^^^^^^^^^ 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. .. code-block:: python >>> from azure.messaging.webpubsubservice import WebPubSubServiceClient >>> from azure.core.credentials import AzureKeyCredential >>> client = WebPubSubServiceClient(endpoint='', credential=AzureKeyCredential('somesecret')) >>> client '> Examples -------- Sending a request ^^^^^^^^^^^^^^^^^ .. code-block:: python >>> 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='', credential=AzureKeyCredential('somesecret')) >>> request = build_send_to_all_request('default', json={ 'Hello': 'webpubsub!' }) >>> request >>> response = client.send_request(request) >>> response >>> 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) 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. .. code-block:: python 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. :raw-html-m2r:`` Indices and tables ------------------ * :ref:`genindex` * :ref:`modindex` * :ref:`search` .. toctree:: :maxdepth: 5 :glob: :caption: Developer Documentation azure.messaging.webpubsubservice.rst