azure-messaging-webpubsubservice-py – skill DevOps & Hạ tầng cho agent AI
Azure Web PubSub Service SDK cho Python. Dùng cho nhắn tin thời gian thực, kết nối WebSocket và mô hình pub/sub.
Cài skill azure-messaging-webpubsubservice-py bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/azure-messaging-webpubsubservice-py-571f0.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /azure-messaging-webpubsubservice-py – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/azure-messaging-webpubsubservice-py-571f0.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $azure-messaging-webpubsubservice-py – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/azure-messaging-webpubsubservice-py-571f0.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /azure-messaging-webpubsubservice-py – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/azure-messaging-webpubsubservice-py-571f0.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /azure-messaging-webpubsubservice-py – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
Azure Web PubSub Service SDK for Python
Real-time messaging with WebSocket connections at scale.
Installation
# Service SDK (server-side)
pip install azure-messaging-webpubsubservice
# Client SDK (for Python WebSocket clients)
pip install azure-messaging-webpubsubclientEnvironment Variables
AZURE_WEBPUBSUB_CONNECTION_STRING=Endpoint=https://<name>.webpubsub.azure.com;AccessKey=...
AZURE_WEBPUBSUB_HUB=my-hubService Client (Server-Side)
Authentication
import os
from azure.messaging.webpubsubservice import WebPubSubServiceClient
# Connection string
client = WebPubSubServiceClient.from_connection_string(
connection_string=os.environ["AZURE_WEBPUBSUB_CONNECTION_STRING"],
hub="my-hub"
)
# Entra ID
from azure.identity import DefaultAzureCredential
client = WebPubSubServiceClient(
endpoint="https://<name>.webpubsub.azure.com",
hub="my-hub",
credential=DefaultAzureCredential()
)Generate Client Access Token
# Token for anonymous user
token = client.get_client_access_token()
# Treat token["url"] as a bearer secret; return it only to the authenticated intended client.
# Token with user ID
token = client.get_client_access_token(
user_id="user123",
roles=["webpubsub.sendToGroup.my-group", "webpubsub.joinLeaveGroup.my-group"]
)
# Token with groups
token = client.get_client_access_token(
user_id="user123",
groups=["group1", "group2"]
)Send to All Clients
# Send text
client.send_to_all(message="Hello everyone!", content_type="text/plain")
# Send JSON
client.send_to_all(
message={"type": "notification", "data": "Hello"},
content_type="application/json"
)Send to User
client.send_to_user(
user_id="user123",
message="Hello user!",
content_type="text/plain"
)Send to Group
client.send_to_group(
group="my-group",
message="Hello group!",
content_type="text/plain"
)Send to Connection
client.send_to_connection(
connection_id="abc123",
message="Hello connection!",
content_type="text/plain"
)Group Management
# Add user to group
client.add_user_to_group(group="my-group", user_id="user123")
# Remove user from group
client.remove_user_from_group(group="my-group", user_id="user123")
# Add connection to group
client.add_connection_to_group(group="my-group", connection_id="abc123")
# Remove connection from group
client.remove_connection_from_group(group="my-group", connection_id="abc123")Connection Management
# Check if connection exists
exists = client.connection_exists(connection_id="abc123")
# Check if user has connections
exists = client.user_exists(user_id="user123")
# Check if group has connections
exists = client.group_exists(group="my-group")
# Close connection
client.close_connection(connection_id="abc123", reason="Session ended")
# Close all connections for user
client.close_all_connections(user_id="user123")Grant/Revoke Permissions
from azure.messaging.webpubsubservice import WebPubSubServiceClient
# Grant permission
client.grant_permission(
permission="joinLeaveGroup",
connection_id="abc123",
target_name="my-group"
)
# Revoke permission
client.revoke_permission(
permission="joinLeaveGroup",
connection_id="abc123",
target_name="my-group"
)
# Check permission
has_permission = client.check_permission(
permission="joinLeaveGroup",
connection_id="abc123",
target_name="my-group"
)Client SDK (Python WebSocket Client)
from azure.messaging.webpubsubclient import WebPubSubClient
from azure.messaging.webpubsubclient.models import CallbackType, WebPubSubDataType
with WebPubSubClient(token["url"]) as ws_client:
ws_client.subscribe(CallbackType.CONNECTED, lambda e: print("Connected"))
ws_client.subscribe(CallbackType.GROUP_MESSAGE, lambda e: print("Group message received"))
ws_client.join_group("my-group")
ws_client.send_to_group("my-group", "Hello from Python!", WebPubSubDataType.TEXT)Authorize token issuance on the server; derive user IDs and allowed groups from the authenticated session, never untrusted request fields. Use a short explicit expiration supported by the pinned SDK, avoid logging access URLs or message bodies, and close service clients and credentials when done. These sends/group changes affect live clients; use a test hub for examples.
Async Service Client
from azure.messaging.webpubsubservice.aio import WebPubSubServiceClient
from azure.identity.aio import DefaultAzureCredential
async def broadcast():
credential = DefaultAzureCredential()
client = WebPubSubServiceClient(
endpoint="https://<name>.webpubsub.azure.com",
hub="my-hub",
credential=credential
)
await client.send_to_all("Hello async!", content_type="text/plain")
await client.close()
await credential.close()Client Operations
| Operation | Description |
|-----------|-------------|
| get_client_access_token | Generate WebSocket connection URL |
| send_to_all | Broadcast to all connections |
| send_to_user | Send to specific user |
| send_to_group | Send to group members |
| send_to_connection | Send to specific connection |
| add_user_to_group | Add user to group |
| remove_user_from_group | Remove user from group |
| close_connection | Disconnect client |
| connection_exists | Check connection status |
Best Practices
1. Use roles to limit client permissions
2. Use groups for targeted messaging
3. Generate short-lived tokens for security
4. Use user IDs to send to users across connections
5. Handle reconnection in client applications
6. Use JSON content type for structured data
7. Close connections gracefully with reasons
When to Use
This skill is applicable to execute the workflow or actions described in the overview.
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
Skill liên quan
- azure-mgmt-apicenter-dotnet – Azure API Center SDK cho .NET. Quản lý tập trung kho API với quản trị, phiên bản hóa và…
- azure-mgmt-apicenter-py – Azure API Center Management SDK cho Python. Dùng để quản lý kho API, metadata và quản…
- azure-mgmt-apimanagement-dotnet – Azure Resource Manager SDK cho API Management trong .NET.
- azure-mgmt-apimanagement-py – Azure API Management SDK cho Python. Dùng để quản lý dịch vụ APIM, API, sản phẩm…
- nextflow – Xây dựng, chạy và gỡ lỗi pipeline Nextflow DSL2 và workflow nf-core. Dùng cho Nextflow…
- azure-mgmt-applicationinsights-dotnet – Azure Application Insights SDK cho .NET. Quản lý tài nguyên giám sát hiệu năng ứng dụng…
- azure-mgmt-arizeaiobservabilityeval-dotnet – SDK Azure Resource Manager cho Arize AI Observability and Evaluation (.NET).
- azure-mgmt-botservice-dotnet – SDK Azure Resource Manager cho Bot Service trong .NET. Thao tác mặt phẳng quản lý để tạo…