Azure Web PubSub for Python: Scalable Real-Time Messaging
Building real-time features like chat, notifications, or live updates traditionally requires managing WebSocket servers, handling reconnections, and scaling connection pools. Azure Web PubSub eliminates this complexity by providing fully managed WebSocket infrastructure.
What This Skill Does
Offers WebSocket connection management, pub/sub messaging, group and user management, broadcast and targeted messaging, connection authentication and authorization, and async client for high-concurrency scenarios.
Getting Started
pip install azure-messaging-webpubsubservice azure-identity
Send messages from server-side Python:
from azure.messaging.webpubsubservice import WebPubSubServiceClient
client = WebPubSubServiceClient.from_connection_string(
connection_string="<connection-string>",
hub="chat"
)
# Broadcast to all
client.send_to_all(message="Hello everyone!", content_type="text/plain")
# Send to user
client.send_to_user(user_id="user123", message="Private message")
# Send to group
client.send_to_group(group="room-1", message="Group notification")
Generate client access URLs:
from azure.messaging.webpubsubservice import WebPubSubServiceClient
token = client.get_client_access_token(user_id="user123", roles=["webpubsub.sendToGroup"])
print(f"WebSocket URL: {token['url']}")
Key Features
Managed Infrastructure eliminates WebSocket server maintenance. Pub/Sub enables efficient message routing. User/Group Model organizes connections. HTTP API sends messages without WebSocket. Async Support for FastAPI and aiohttp integration.
When to Use
Use for real-time chat, live dashboards, collaborative applications, push notifications, streaming telemetry, and multiplayer game state synchronization.
Source
Maintained by Microsoft. View on GitHub