diff --git a/sdk/webpubsub/azure-messaging-webpubsubservice/README.md b/sdk/webpubsub/azure-messaging-webpubsubservice/README.md index 9694d7259937..525883a687c2 100644 --- a/sdk/webpubsub/azure-messaging-webpubsubservice/README.md +++ b/sdk/webpubsub/azure-messaging-webpubsubservice/README.md @@ -83,21 +83,39 @@ When the client is connected, it can send messages to the upstream application, ## Examples -### Broadcast messages +### Broadcast messages in JSON format ```python >>> from azure.messaging.webpubsubservice import WebPubSubServiceClient ->>> from azure.identity import DefaultAzureCredential ->>> from azure.core.exceptions import HttpResponseError ->>> service = WebPubSubServiceClient(endpoint='', credential=DefaultAzureCredential()) ->>> with open('file.json', 'r') as f: - try: - service.send_to_all('ahub', content=f) - except HttpResponseError as e: - print('service responds error: {}'.format(e.response.json())) +>>> service = WebPubSubServiceClient.from_connection_string('') +>>> service.send_to_all('hub1', 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('') +>>> service.send_to_all('hub1', 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('') +>>> service.send_to_all('hub1', message=io.StringIO('Hello World'), content_type='application/octet-stream') ``` +The WebSocket client will receive binary text: `b'Hello world'`. ## Troubleshooting @@ -138,7 +156,7 @@ Http request and response details are printed to stdout with this logging config ## Next steps -Check [more samples here][awps_samples]. +Check [more samples here][samples]. ## Contributing @@ -178,4 +196,4 @@ additional questions or comments. [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 -[awps_samples]: https://github.com/Azure/azure-webpubsub/tree/main/samples/python +[samples]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/webpubsub/azure-messaging-webpubsubservice/samples diff --git a/sdk/webpubsub/azure-messaging-webpubsubservice/samples/Readme.md b/sdk/webpubsub/azure-messaging-webpubsubservice/samples/Readme.md new file mode 100644 index 000000000000..a87a9d447d4b --- /dev/null +++ b/sdk/webpubsub/azure-messaging-webpubsubservice/samples/Readme.md @@ -0,0 +1,51 @@ +--- +page_type: sample +languages: + - python +products: + - azure + - azure-messaging-webpubsubservice +urlFragment: azure-messaging-webpubsubservice-samples +--- + +# Azure Web PubSub service client library for Python Samples + +These are code samples that show common scenario operations with the Azure Storage Blob client library. +The async versions of the samples (the python sample files appended with `_async`) show asynchronous operations, +and require Python 3.5 or later. + +## Prerequisites +* Python 2.7, or 3.5 or later is required to use this package (3.5 or later if using asyncio) +* You need an [Azure subscription][azure_sub], and a [Azure WebPubSub service instance][webpubsubservice_docs] to use this package. + +## Setup + +1. Install the Azure Web PubSub service client library for Python with [pip](https://pypi.org/project/pip/): + +```bash +pip install azure-messaging-webpubsubservice +``` + +2. Clone or download this sample repository +3. Open the sample folder in Visual Studio Code or your IDE of choice. + +## Running the samples + +1. Open a terminal window and `cd` to the directory that the samples are saved in. +2. Set the environment variables specified in the sample file you wish to run. +3. Follow the usage described in the file, e.g. `python get_client_access_token.py` + +## More samples + +Check [more samples here][servicesample]. + +## Next Steps + +Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. + + + +[azure_sub]: https://azure.microsoft.com/free/ +[webpubsubservice_docs]: https://aka.ms/awps/doc +[servicesample]: https://github.com/Azure/azure-webpubsub/tree/main/samples/python +[apiref]: https://aka.ms/awps/sdk/python diff --git a/sdk/webpubsub/azure-messaging-webpubsubservice/setup.py b/sdk/webpubsub/azure-messaging-webpubsubservice/setup.py index 6a34ea471e9e..984e0e81b5d9 100644 --- a/sdk/webpubsub/azure-messaging-webpubsubservice/setup.py +++ b/sdk/webpubsub/azure-messaging-webpubsubservice/setup.py @@ -39,7 +39,7 @@ license="MIT License", author="Microsoft Corporation", author_email="azpysdkhelp@microsoft.com", - url="https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/signalr/azure-messaging-webpubsubservice", + url="https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/webpubsub/azure-messaging-webpubsubservice", classifiers=[ "Development Status :: 4 - Beta", "Programming Language :: Python",