# Azure Web PubSub service client library for Python
[Azure Web PubSub Service](https://aka.ms/awps/doc) is an Azure-managed service that helps developers easily build web applications with real-time features and publish-subscribe pattern. 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.
You can use this library in your app server side to manage the WebSocket client connections, as shown in below diagram:
![overflow](https://user-images.githubusercontent.com/668244/140014067-25a00959-04dc-47e8-ac25-6957bd0a71ce.png)
Use this 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, and check permissions for an existing connection
[Source code](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/webpubsub/azure-messaging-webpubsubservice)
| [Package (Pypi)][package]
| [Package (Conda)](https://anaconda.org/microsoft/azure-messaging-webpubsubservice)
| [API reference documentation](https://aka.ms/awps/sdk/python)
| [Product documentation][webpubsubservice_docs]
## _Disclaimer_
_Azure SDK Python packages support for Python 2.7 has ended 01 January 2022. For more information and questions, please refer to https://github.com/Azure/azure-sdk-for-python/issues/20691_
## Getting started
### Prerequisites
- Python 3.6 or later is required to use this package.
- You need an [Azure subscription][azure_sub], and a [Azure WebPubSub service instance][webpubsubservice_docs] to use this package.
- An existing Azure Web PubSub service instance.
### 1. Install the package
```bash
python -m pip install azure-messaging-webpubsubservice
```
### 2. Create and authenticate a WebPubSubServiceClient
You can authenticate the `WebPubSubServiceClient` using [connection string][connection_string]:
```python
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> service = WebPubSubServiceClient.from_connection_string(connection_string='<connection_string>', hub='hub')
```
Or using the service endpoint and the access key:
```python
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> from azure.core.credentials import AzureKeyCredential
>>> service = WebPubSubServiceClient(endpoint='<endpoint>', hub='hub', credential=AzureKeyCredential("<access_key>"))
```
Or using [Azure Active Directory][aad_doc]:
1. [pip][pip] install [`azure-identity`][azure_identity_pip]
2. Follow the document to [enable AAD authentication on your Webpubsub resource][aad_doc]
3. Update code to use [DefaultAzureCredential][default_azure_credential]
```python
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> from azure.identity import DefaultAzureCredential
>>> service = WebPubSubServiceClient(endpoint='<endpoint>', hub='hub', credential=DefaultAzureCredential())
```
## Key concepts
### Connection
A connection, also known as a client or a client connection, represents an individual WebSocket connection connected to the Web PubSub service. When successfully connected, a unique connection ID is assigned to this connection by the Web PubSub service.
### Hub
A hub is a logical concept for a set of client connections. Usually you use one hub for one purpose, for example, a chat hub, or a notification hub. When a client connection is created, it connects to a hub, and during its lifetime, it belongs to that hub. Different applications can share one Azure Web PubSub service by using different hub names.
### Group
A group is a subset of connections to the hub. You can add a client connection to a group, or remove the client connection from the group, anytime you want. For example, when a client joins a chat room, or when a client leaves the chat room, this chat room can be considered to be a group. 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
When the client is connected, it can send messages to the upstream application, or receive messages from the upstream application, through the WebSocket connection.
## Examples
### Broadcast messages in JSON format
```python
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> service = WebPubSubServiceClient.from_connection_string('<connection_string>', hub='hub1')
>>> service.send_to_all(message = {
'from': 'user1',
'data': 'Hello world'
})
```
The WebSocket client will receive JSON serialized text: `{"from": "user1", "data": "Hello world"}`.
### Broadcast messages in plain-text format
```python
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> service = WebPubSubServiceClient.from_connection_string('<connection_string>', hub='hub1')
>>> service.send_to_all(message = 'Hello world', content_type='text/plain')
```
The WebSocket client will receive text: `Hello world`.
### Broadcast messages in binary format
```python
>>> import io
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> service = WebPubSubServiceClient.from_connection_string('<connection_string>', hub='hub')
>>> service.send_to_all(message=io.StringIO('Hello World'), content_type='application/octet-stream')
```
The WebSocket client will receive binary text: `b'Hello world'`.
## Troubleshooting
### Logging
This SDK uses Python standard logging library.
You can configure logging print out debugging information to the stdout or anywhere you want.
```python
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 WebPubSubServiceClient will log detailed information about its HTTP sessions, at DEBUG level
service = WebPubSubServiceClient(endpoint=endpoint, hub='hub', credential=credential, logging_enable=True)
```
Similarly, `logging_enable` can enable detailed logging for a single call,
even when it isn't enabled for the WebPubSubServiceClient:
```python
result = service.send_to_all(..., logging_enable=True)
```
Http request and response details are printed to stdout with this logging config.
## Next steps
Check [more samples here][samples].
## 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][code_of_conduct]. For more information,
see the Code of Conduct FAQ or contact opencode@microsoft.com with any
additional questions or comments.
<!-- LINKS -->
[webpubsubservice_docs]: https://aka.ms/awps/doc
[azure_cli]: https://docs.microsoft.com/cli/azure
[azure_sub]: https://azure.microsoft.com/free/
[package]: https://pypi.org/project/azure-messaging-webpubsubservice/
[default_cred_ref]: https://aka.ms/azsdk-python-identity-default-cred-ref
[cla]: https://cla.microsoft.com
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/
[coc_contact]: mailto:opencode@microsoft.com
[authenticate_with_token]: https://docs.microsoft.com/azure/cognitive-services/authentication?tabs=powershell#authenticate-with-an-authentication-token
[azure_identity_credentials]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credentials
[azure_identity_pip]: https://pypi.org/project/azure-identity/
[default_azure_credential]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#defaultazurecredential
[pip]: https://pypi.org/project/pip/
[enable_aad]: https://docs.microsoft.com/azure/azure-web-pubsub/howto-develop-create-instance
[api_key]: https://docs.microsoft.com/azure/azure-web-pubsub/howto-websocket-connect?tabs=browser#authorization
[connection_string]: https://docs.microsoft.com/azure/azure-web-pubsub/howto-websocket-connect?tabs=browser#authorization
[azure_portal]: https://docs.microsoft.com/azure/azure-web-pubsub/howto-develop-create-instance
[azure-key-credential]: https://aka.ms/azsdk-python-core-azurekeycredential
[aad_doc]: https://aka.ms/awps/aad
[samples]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/webpubsub/azure-messaging-webpubsubservice/samples
Raw data
{
"_id": null,
"home_page": "https://github.com/Azure/azure-sdk-for-python/tree/main/sdk",
"name": "azure-messaging-webpubsubservice",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "azure, azure sdk",
"author": "Microsoft Corporation",
"author_email": "azpysdkhelp@microsoft.com",
"download_url": "https://files.pythonhosted.org/packages/8a/13/90f90a7b8bbcd5b67e9e2d0bf2ed2e827768414e3897a4d931e2f6f655e5/azure-messaging-webpubsubservice-1.2.0.tar.gz",
"platform": null,
"description": "# Azure Web PubSub service client library for Python\n\n[Azure Web PubSub Service](https://aka.ms/awps/doc) is an Azure-managed service that helps developers easily build web applications with real-time features and publish-subscribe pattern. 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.\n\nYou can use this library in your app server side to manage the WebSocket client connections, as shown in below diagram:\n\n![overflow](https://user-images.githubusercontent.com/668244/140014067-25a00959-04dc-47e8-ac25-6957bd0a71ce.png)\n\nUse this library to:\n- Send messages to hubs and groups.\n- Send messages to particular users and connections.\n- Organize users and connections into groups.\n- Close connections\n- Grant, revoke, and check permissions for an existing connection\n\n[Source code](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/webpubsub/azure-messaging-webpubsubservice)\n| [Package (Pypi)][package]\n| [Package (Conda)](https://anaconda.org/microsoft/azure-messaging-webpubsubservice)\n| [API reference documentation](https://aka.ms/awps/sdk/python)\n| [Product documentation][webpubsubservice_docs]\n\n## _Disclaimer_\n\n_Azure SDK Python packages support for Python 2.7 has ended 01 January 2022. For more information and questions, please refer to https://github.com/Azure/azure-sdk-for-python/issues/20691_\n\n## Getting started\n\n### Prerequisites\n\n- Python 3.6 or later is required to use this package.\n- You need an [Azure subscription][azure_sub], and a [Azure WebPubSub service instance][webpubsubservice_docs] to use this package.\n- An existing Azure Web PubSub service instance.\n\n### 1. Install the package\n\n```bash\npython -m pip install azure-messaging-webpubsubservice\n```\n\n### 2. Create and authenticate a WebPubSubServiceClient\n\nYou can authenticate the `WebPubSubServiceClient` using [connection string][connection_string]:\n\n```python\n>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient\n\n>>> service = WebPubSubServiceClient.from_connection_string(connection_string='<connection_string>', hub='hub')\n```\n\nOr using the service endpoint and the access key:\n\n```python\n>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient\n>>> from azure.core.credentials import AzureKeyCredential\n\n>>> service = WebPubSubServiceClient(endpoint='<endpoint>', hub='hub', credential=AzureKeyCredential(\"<access_key>\"))\n```\n\nOr using [Azure Active Directory][aad_doc]:\n1. [pip][pip] install [`azure-identity`][azure_identity_pip]\n2. Follow the document to [enable AAD authentication on your Webpubsub resource][aad_doc]\n3. Update code to use [DefaultAzureCredential][default_azure_credential]\n\n ```python\n >>> from azure.messaging.webpubsubservice import WebPubSubServiceClient\n >>> from azure.identity import DefaultAzureCredential\n >>> service = WebPubSubServiceClient(endpoint='<endpoint>', hub='hub', credential=DefaultAzureCredential())\n ```\n\n## Key concepts\n\n### Connection\n\nA connection, also known as a client or a client connection, represents an individual WebSocket connection connected to the Web PubSub service. When successfully connected, a unique connection ID is assigned to this connection by the Web PubSub service.\n\n### Hub\n\nA hub is a logical concept for a set of client connections. Usually you use one hub for one purpose, for example, a chat hub, or a notification hub. When a client connection is created, it connects to a hub, and during its lifetime, it belongs to that hub. Different applications can share one Azure Web PubSub service by using different hub names.\n\n### Group\n\nA group is a subset of connections to the hub. You can add a client connection to a group, or remove the client connection from the group, anytime you want. For example, when a client joins a chat room, or when a client leaves the chat room, this chat room can be considered to be a group. A client can join multiple groups, and a group can contain multiple clients.\n\n### User\n\nConnections 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.\n\n### Message\n\nWhen the client is connected, it can send messages to the upstream application, or receive messages from the upstream application, through the WebSocket connection.\n\n## Examples\n\n### Broadcast messages in JSON format\n\n```python\n>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient\n\n>>> service = WebPubSubServiceClient.from_connection_string('<connection_string>', hub='hub1')\n>>> service.send_to_all(message = {\n 'from': 'user1',\n 'data': 'Hello world'\n })\n```\n\nThe WebSocket client will receive JSON serialized text: `{\"from\": \"user1\", \"data\": \"Hello world\"}`.\n\n### Broadcast messages in plain-text format\n\n```python\n>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient\n>>> service = WebPubSubServiceClient.from_connection_string('<connection_string>', hub='hub1')\n>>> service.send_to_all(message = 'Hello world', content_type='text/plain')\n```\n\nThe WebSocket client will receive text: `Hello world`.\n\n### Broadcast messages in binary format\n\n```python\n>>> import io\n>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient\n>>> service = WebPubSubServiceClient.from_connection_string('<connection_string>', hub='hub')\n>>> service.send_to_all(message=io.StringIO('Hello World'), content_type='application/octet-stream')\n```\nThe WebSocket client will receive binary text: `b'Hello world'`.\n\n## Troubleshooting\n\n### Logging\n\nThis SDK uses Python standard logging library.\nYou can configure logging print out debugging information to the stdout or anywhere you want.\n\n```python\nimport sys\nimport logging\nfrom azure.identity import DefaultAzureCredential\n>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient\n\n# Create a logger for the 'azure' SDK\nlogger = logging.getLogger('azure')\nlogger.setLevel(logging.DEBUG)\n\n# Configure a console output\nhandler = logging.StreamHandler(stream=sys.stdout)\nlogger.addHandler(handler)\n\nendpoint = \"<endpoint>\"\ncredential = DefaultAzureCredential()\n\n# This WebPubSubServiceClient will log detailed information about its HTTP sessions, at DEBUG level\nservice = WebPubSubServiceClient(endpoint=endpoint, hub='hub', credential=credential, logging_enable=True)\n```\n\nSimilarly, `logging_enable` can enable detailed logging for a single call,\neven when it isn't enabled for the WebPubSubServiceClient:\n\n```python\nresult = service.send_to_all(..., logging_enable=True)\n```\n\nHttp request and response details are printed to stdout with this logging config.\n\n## Next steps\n\nCheck [more samples here][samples].\n\n## Contributing\n\nThis project welcomes contributions and suggestions. Most contributions require\nyou to agree to a Contributor License Agreement (CLA) declaring that you have\nthe right to, and actually do, grant us the rights to use your contribution.\nFor details, visit https://cla.microsoft.com.\n\nWhen you submit a pull request, a CLA-bot will automatically determine whether\nyou need to provide a CLA and decorate the PR appropriately (e.g., label,\ncomment). Simply follow the instructions provided by the bot. You will only\nneed to do this once across all repos using our CLA.\n\nThis project has adopted the\n[Microsoft Open Source Code of Conduct][code_of_conduct]. For more information,\nsee the Code of Conduct FAQ or contact opencode@microsoft.com with any\nadditional questions or comments.\n\n<!-- LINKS -->\n[webpubsubservice_docs]: https://aka.ms/awps/doc\n[azure_cli]: https://docs.microsoft.com/cli/azure\n[azure_sub]: https://azure.microsoft.com/free/\n[package]: https://pypi.org/project/azure-messaging-webpubsubservice/\n[default_cred_ref]: https://aka.ms/azsdk-python-identity-default-cred-ref\n[cla]: https://cla.microsoft.com\n[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/\n[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/\n[coc_contact]: mailto:opencode@microsoft.com\n[authenticate_with_token]: https://docs.microsoft.com/azure/cognitive-services/authentication?tabs=powershell#authenticate-with-an-authentication-token\n[azure_identity_credentials]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credentials\n[azure_identity_pip]: https://pypi.org/project/azure-identity/\n[default_azure_credential]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#defaultazurecredential\n[pip]: https://pypi.org/project/pip/\n[enable_aad]: https://docs.microsoft.com/azure/azure-web-pubsub/howto-develop-create-instance\n[api_key]: https://docs.microsoft.com/azure/azure-web-pubsub/howto-websocket-connect?tabs=browser#authorization\n[connection_string]: https://docs.microsoft.com/azure/azure-web-pubsub/howto-websocket-connect?tabs=browser#authorization\n[azure_portal]: https://docs.microsoft.com/azure/azure-web-pubsub/howto-develop-create-instance\n[azure-key-credential]: https://aka.ms/azsdk-python-core-azurekeycredential\n[aad_doc]: https://aka.ms/awps/aad\n[samples]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/webpubsub/azure-messaging-webpubsubservice/samples\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "Microsoft Azure WebPubSub Service Client Library for Python",
"version": "1.2.0",
"project_urls": {
"Homepage": "https://github.com/Azure/azure-sdk-for-python/tree/main/sdk"
},
"split_keywords": [
"azure",
" azure sdk"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "eeb71461cafb270fe58644c61f379ea05c39dcd1fa5dc014b431b157a980e4a8",
"md5": "39957cb321e311f02483e891d48e1e35",
"sha256": "30fd24d2d05031eb3ac1ef2516adb4589eb88f1613c3d0519befa4d8f9547104"
},
"downloads": -1,
"filename": "azure_messaging_webpubsubservice-1.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "39957cb321e311f02483e891d48e1e35",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 55893,
"upload_time": "2024-08-09T06:20:17",
"upload_time_iso_8601": "2024-08-09T06:20:17.812564Z",
"url": "https://files.pythonhosted.org/packages/ee/b7/1461cafb270fe58644c61f379ea05c39dcd1fa5dc014b431b157a980e4a8/azure_messaging_webpubsubservice-1.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "8a1390f90a7b8bbcd5b67e9e2d0bf2ed2e827768414e3897a4d931e2f6f655e5",
"md5": "a406b2fb3d7466798cee1c91226a99ec",
"sha256": "a842e4280cefab7bd973eb7c8cc364f28711998254b38219c2da52ef4b8d6c15"
},
"downloads": -1,
"filename": "azure-messaging-webpubsubservice-1.2.0.tar.gz",
"has_sig": false,
"md5_digest": "a406b2fb3d7466798cee1c91226a99ec",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 58053,
"upload_time": "2024-08-09T06:20:16",
"upload_time_iso_8601": "2024-08-09T06:20:16.447621Z",
"url": "https://files.pythonhosted.org/packages/8a/13/90f90a7b8bbcd5b67e9e2d0bf2ed2e827768414e3897a4d931e2f6f655e5/azure-messaging-webpubsubservice-1.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-09 06:20:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Azure",
"github_project": "azure-sdk-for-python",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "azure-messaging-webpubsubservice"
}