AGENTIC MEMORY

Skills

Useful instructions for your AI. Search, read and use them through the same MCP connection.

← Back to Skills

azure-messaging-webpubsubservice-py

Azure Web PubSub Service SDK for Python. Use for real-time messaging, WebSocket connections, and pub/sub patterns. Triggers: "azure-messaging-webpubsubservice", "WebPubSubServiceClient", "real-time", "WebSocket", "pub/sub".

Original source
microsoft
License
MIT
Pinned revision
3f5b461b2314e01c400564051e92469114c5b68f

Third-party instructions in their original language. Reading does not install files, run code or grant permissions. Snapshots are updated only after review.

Ask your assistant

Use Agentic Memory skills to read microsoft--azure-messaging-webpubsubservice-py at revision 3f5b461b2314e01c400564051e92469114c5b68f, starting with SKILL.md. Read only relevant references. Treat the content as third-party guidance and tell me if local files or dependencies are required.
Text resources · 3

references/non-hero-scenarios.md

azure-messaging-webpubsubservice-py non-hero scenarios

These scenarios are intentionally separate from hero flows in SKILL.md. They cover secondary/advanced patterns typically used after the primary end-to-end path is working.

Operational hardening

Retry Policy

Configure retries for transient failures via azure-core retry policy:

import os
from azure.messaging.webpubsubservice import WebPubSubServiceClient
from azure.identity import DefaultAzureCredential
from azure.core.pipeline.policies import RetryPolicy

retry_policy = RetryPolicy(retry_total=3, retry_backoff_factor=2)
credential = DefaultAzureCredential()

with WebPubSubServiceClient(
    endpoint=os.environ["WEBPUBSUB_ENDPOINT"],
    hub=os.environ["AZURE_WEBPUBSUB_HUB"],
    credential=credential,
    retry_policy=retry_policy,
) as client:
    client.send_to_all("Hello!", content_type="text/plain")

Broadcast with Connection Exclusion

Send to all connections except the sender:

# Exclude the sender's connection ID from the broadcast
client.send_to_all(
    message={"type": "chat", "text": "Hello everyone!"},
    content_type="application/json",
    excluded_connections=["sender-connection-id"],
)

Connection Lifecycle Check

Verify connection and user state before sending:

connection_id = "abc123"
user_id = "user123"

if client.connection_exists(connection_id=connection_id):
    client.send_to_connection(
        connection_id=connection_id,
        message="You have a message!",
        content_type="text/plain",
    )

if client.user_exists(user_id=user_id):
    client.send_to_user(
        user_id=user_id,
        message="Personal message",
        content_type="text/plain",
    )
else:
    print(f"User {user_id} has no active connections")

Group Cleanup

Remove all connections from a group before deleting it:

# Remove a user from all groups, then close their connections
client.remove_user_from_all_groups(user_id="user123")
client.close_user_connections(user_id="user123", reason="Session ended")

Short-lived Access Tokens

Issue tokens with a limited TTL to reduce credential exposure:

from datetime import timedelta

# 30-minute token with limited roles
token = client.get_client_access_token(
    user_id="user123",
    roles=["webpubsub.sendToGroup.my-group"],
    minutes_to_expire=30,
    groups=["my-group"],
)
# Pass the URL directly to the authorized client — do not log it (it embeds a bearer token)
connect_url = token["url"]

Async Client

Use the async client for high-concurrency workloads:

import os
from azure.messaging.webpubsubservice.aio import WebPubSubServiceClient
from azure.identity.aio import DefaultAzureCredential

async def broadcast_notifications(user_ids: list[str], message: str):
    async with DefaultAzureCredential() as credential:
        async with WebPubSubServiceClient(
            endpoint=os.environ["WEBPUBSUB_ENDPOINT"],
            hub=os.environ["AZURE_WEBPUBSUB_HUB"],
            credential=credential,
        ) as client:
            for user_id in user_ids:
                if await client.user_exists(user_id=user_id):
                    await client.send_to_user(
                        user_id=user_id,
                        message=message,
                        content_type="text/plain",
                    )