|
Add this skill
npx mdskills install sickn33/azure-messaging-webpubsubservice-pyComprehensive Azure WebSocket SDK reference with clear examples but lacks agent-specific instructions
1---2name: azure-messaging-webpubsubservice-py3description: |4 Azure Web PubSub Service SDK for Python. Use for real-time messaging, WebSocket connections, and pub/sub patterns.5 Triggers: "azure-messaging-webpubsubservice", "WebPubSubServiceClient", "real-time", "WebSocket", "pub/sub".6package: azure-messaging-webpubsubservice7---89# Azure Web PubSub Service SDK for Python1011Real-time messaging with WebSocket connections at scale.1213## Installation1415```bash16# Service SDK (server-side)17pip install azure-messaging-webpubsubservice1819# Client SDK (for Python WebSocket clients)20pip install azure-messaging-webpubsubclient21```2223## Environment Variables2425```bash26AZURE_WEBPUBSUB_CONNECTION_STRING=Endpoint=https://<name>.webpubsub.azure.com;AccessKey=...27AZURE_WEBPUBSUB_HUB=my-hub28```2930## Service Client (Server-Side)3132### Authentication3334```python35from azure.messaging.webpubsubservice import WebPubSubServiceClient3637# Connection string38client = WebPubSubServiceClient.from_connection_string(39 connection_string=os.environ["AZURE_WEBPUBSUB_CONNECTION_STRING"],40 hub="my-hub"41)4243# Entra ID44from azure.identity import DefaultAzureCredential4546client = WebPubSubServiceClient(47 endpoint="https://<name>.webpubsub.azure.com",48 hub="my-hub",49 credential=DefaultAzureCredential()50)51```5253### Generate Client Access Token5455```python56# Token for anonymous user57token = client.get_client_access_token()58print(f"URL: {token['url']}")5960# Token with user ID61token = client.get_client_access_token(62 user_id="user123",63 roles=["webpubsub.sendToGroup", "webpubsub.joinLeaveGroup"]64)6566# Token with groups67token = client.get_client_access_token(68 user_id="user123",69 groups=["group1", "group2"]70)71```7273### Send to All Clients7475```python76# Send text77client.send_to_all(message="Hello everyone!", content_type="text/plain")7879# Send JSON80client.send_to_all(81 message={"type": "notification", "data": "Hello"},82 content_type="application/json"83)84```8586### Send to User8788```python89client.send_to_user(90 user_id="user123",91 message="Hello user!",92 content_type="text/plain"93)94```9596### Send to Group9798```python99client.send_to_group(100 group="my-group",101 message="Hello group!",102 content_type="text/plain"103)104```105106### Send to Connection107108```python109client.send_to_connection(110 connection_id="abc123",111 message="Hello connection!",112 content_type="text/plain"113)114```115116### Group Management117118```python119# Add user to group120client.add_user_to_group(group="my-group", user_id="user123")121122# Remove user from group123client.remove_user_from_group(group="my-group", user_id="user123")124125# Add connection to group126client.add_connection_to_group(group="my-group", connection_id="abc123")127128# Remove connection from group129client.remove_connection_from_group(group="my-group", connection_id="abc123")130```131132### Connection Management133134```python135# Check if connection exists136exists = client.connection_exists(connection_id="abc123")137138# Check if user has connections139exists = client.user_exists(user_id="user123")140141# Check if group has connections142exists = client.group_exists(group="my-group")143144# Close connection145client.close_connection(connection_id="abc123", reason="Session ended")146147# Close all connections for user148client.close_all_connections(user_id="user123")149```150151### Grant/Revoke Permissions152153```python154from azure.messaging.webpubsubservice import WebPubSubServiceClient155156# Grant permission157client.grant_permission(158 permission="joinLeaveGroup",159 connection_id="abc123",160 target_name="my-group"161)162163# Revoke permission164client.revoke_permission(165 permission="joinLeaveGroup",166 connection_id="abc123",167 target_name="my-group"168)169170# Check permission171has_permission = client.check_permission(172 permission="joinLeaveGroup",173 connection_id="abc123",174 target_name="my-group"175)176```177178## Client SDK (Python WebSocket Client)179180```python181from azure.messaging.webpubsubclient import WebPubSubClient182183client = WebPubSubClient(credential=token["url"])184185# Event handlers186@client.on("connected")187def on_connected(e):188 print(f"Connected: {e.connection_id}")189190@client.on("server-message")191def on_message(e):192 print(f"Message: {e.data}")193194@client.on("group-message")195def on_group_message(e):196 print(f"Group {e.group}: {e.data}")197198# Connect and send199client.open()200client.send_to_group("my-group", "Hello from Python!")201```202203## Async Service Client204205```python206from azure.messaging.webpubsubservice.aio import WebPubSubServiceClient207from azure.identity.aio import DefaultAzureCredential208209async def broadcast():210 credential = DefaultAzureCredential()211 client = WebPubSubServiceClient(212 endpoint="https://<name>.webpubsub.azure.com",213 hub="my-hub",214 credential=credential215 )216217 await client.send_to_all("Hello async!", content_type="text/plain")218219 await client.close()220 await credential.close()221```222223## Client Operations224225| Operation | Description |226|-----------|-------------|227| `get_client_access_token` | Generate WebSocket connection URL |228| `send_to_all` | Broadcast to all connections |229| `send_to_user` | Send to specific user |230| `send_to_group` | Send to group members |231| `send_to_connection` | Send to specific connection |232| `add_user_to_group` | Add user to group |233| `remove_user_from_group` | Remove user from group |234| `close_connection` | Disconnect client |235| `connection_exists` | Check connection status |236237## Best Practices2382391. **Use roles** to limit client permissions2402. **Use groups** for targeted messaging2413. **Generate short-lived tokens** for security2424. **Use user IDs** to send to users across connections2435. **Handle reconnection** in client applications2446. **Use JSON** content type for structured data2457. **Close connections** gracefully with reasons246
Full transparency — inspect the skill content before installing.