{"id":5748,"library":"azure-messaging-webpubsubservice","title":"Azure WebPubSub Service Client Library for Python","description":"The Azure Web PubSub service client library for Python enables developers to build real-time web applications using WebSockets and the publish-subscribe pattern. It provides server-side APIs to manage WebSocket client connections, send messages to clients, groups, or specific users, and control permissions. The library is actively maintained as part of the Azure SDK for Python, with a rapid release cadence, and the current stable version is 1.3.0.","status":"active","version":"1.3.0","language":"en","source_language":"en","source_url":"https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/webpubsub/azure-messaging-webpubsubservice","tags":["azure","webpubsub","messaging","websockets","real-time","cloud","sdk"],"install":[{"cmd":"pip install azure-messaging-webpubsubservice","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for Azure Active Directory (AAD) authentication using `DefaultAzureCredential` or other `TokenCredential` types.","package":"azure-identity","optional":true}],"imports":[{"symbol":"WebPubSubServiceClient","correct":"from azure.messaging.webpubsubservice import WebPubSubServiceClient"},{"note":"The async client is found in the `.aio` submodule and typically aliased for clarity, or used directly if you prefer the original name from `aio`.","symbol":"WebPubSubServiceAsyncClient","correct":"from azure.messaging.webpubsubservice.aio import WebPubSubServiceClient"}],"quickstart":{"code":"import os\nfrom azure.messaging.webpubsubservice import WebPubSubServiceClient\n\n# --- Authentication --- \n# Option 1: Connection string (recommended for quickstart/dev)\n# Set environment variable AZURE_WEBPUBSUB_CONNECTION_STRING\nconnection_string = os.environ.get('AZURE_WEBPUBSUB_CONNECTION_STRING', 'Endpoint=https://<your-service>.webpubsub.azure.com;AccessKey=<your-access-key>;Version=1.0;')\n# Set environment variable AZURE_WEBPUBSUB_HUB\nhub_name = os.environ.get('AZURE_WEBPUBSUB_HUB', 'myHub')\n\nif not connection_string or 'Endpoint=' not in connection_string:\n    print(\"Please set the AZURE_WEBPUBSUB_CONNECTION_STRING environment variable or provide a valid connection string.\")\n    exit(1)\nif not hub_name or hub_name == 'myHub':\n    print(\"Please set the AZURE_WEBPUBSUB_HUB environment variable or provide a valid hub name.\")\n    exit(1)\n\n# Create a WebPubSubServiceClient instance\nservice_client = WebPubSubServiceClient.from_connection_string(connection_string, hub=hub_name)\n\n# --- Send a message to all clients in the hub ---\ntry:\n    print(f\"Sending 'Hello World' to all clients in hub '{hub_name}'...\")\n    response = service_client.send_to_all(\"Hello World\", content_type='text/plain')\n    print(f\"Message sent. Response: {response}\")\n\n    # Example: Send a JSON message\n    # response = service_client.send_to_all({'message': 'Hello from Python!', 'type': 'greeting'}, content_type='application/json')\n    # print(f\"JSON message sent. Response: {response}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to instantiate `WebPubSubServiceClient` using a connection string from an environment variable and send a plain text message to all connected clients within a specified hub. Ensure `AZURE_WEBPUBSUB_CONNECTION_STRING` and `AZURE_WEBPUBSUB_HUB` environment variables are set for authentication and hub selection, respectively."},"warnings":[{"fix":"Upgrade your Python environment to version 3.8 or higher.","message":"Support for Python 2.7 has officially ended as of January 1, 2022. Users on Python 2.7 will not receive updates and should migrate to Python 3.6+ (or preferably 3.8+ as per current PyPI requirements).","severity":"breaking","affected_versions":"<=1.0.0b2"},{"fix":"Install `azure-identity` and configure AAD on your Web PubSub resource, or use connection string/access key for simpler authentication.","message":"When using Azure Active Directory (AAD) for authentication with `DefaultAzureCredential`, you must install the `azure-identity` package separately (`pip install azure-identity`) and enable AAD authentication on your Azure Web PubSub resource. Direct use of connection strings is simpler for initial setup but less secure for production.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For asynchronous applications, use `WebPubSubServiceAsyncClient` and ensure all calls are awaited within an `asyncio` event loop. For synchronous applications, stick to `WebPubSubServiceClient`.","message":"The SDK provides both synchronous (`WebPubSubServiceClient`) and asynchronous (`WebPubSubServiceAsyncClient` from `azure.messaging.webpubsubservice.aio`) clients. Mixing sync and async operations without proper `asyncio` context can lead to unexpected behavior or deadlocks. Choose the appropriate client for your application's concurrency model.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Implement `try-except HttpResponseError` blocks around API calls to handle potential server-side validation or other HTTP errors.","message":"Error handling for server-side issues will often involve `azure.core.exceptions.HttpResponseError` rather than client-side `ValidationError`. This means you need to catch these exceptions for robust error handling, especially for issues like malformed requests or service-side failures.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}