All skills
microsoft avatar

/azure-messaging-webpubsubservice-py

@e19efc2
by microsoftmicrosoft/skills3.1k stars
351

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".

Use this Skill: https://skilld.dev/gh/microsoft/skills/azure-messaging-webpubsubservice-py

This session only. Nothing lands on disk.

referencesnon-hero-scenarios.md

≈850 tokens on demand. Your agent reads this file only when SKILL.md points to it.

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",
                    )

Source: SKILL.md on GitHub

1 warning15d4 checks · Risk SAFE
  • Gen Agent Trust Hub15d

    This skill is a standard SDK implementation for Azure Web PubSub, following security best practices such as using managed identities. It includes a potential consideration regarding the handling of incoming real-time messages, which is typical for messaging applications.

  • Socket15d

    No alerts

  • Snyk15d

    Risk: LOW · No issues

  • Runlayer7mo

    2/2 files flagged

Signed by skilld at e19efc2. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 3 months ago
Other metadata
metadata
{
  "author": "Microsoft",
  "version": "1.0.0",
  "package": "azure-messaging-webpubsubservice"
}

README badge

README badge for microsoft/skills/azure-messaging-webpubsubservice-py