Integrate Real-Time Messaging with Azure Web PubSub
Java skill for Azure Web PubSub - broadcasting, group/user messaging, connection management, and access tokens.
Why it matters
Enable real-time communication in your Java applications by integrating with Azure Web PubSub. This asset provides the SDK and patterns to efficiently send messages to all connections, specific groups, or individual users.
Outcomes
What it gets done
Send messages to all connected clients.
Manage group memberships for targeted messaging.
Generate client access tokens for secure connections.
Handle asynchronous messaging operations.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/ag-azure-messaging-webpubsub-java | bash Overview
Azure Web PubSub SDK for Java
Java skill for the Azure Web PubSub SDK, covering broadcast/group/user/connection-targeted messaging, group and connection lifecycle management, and client access token generation scoped with roles, groups, and expiration. Use when building the service-side backend for a real-time WebSocket application - chat, notifications, dashboards - on Azure Web PubSub.
What it does
This skill builds real-time web applications with the Azure Web PubSub SDK for Java (com.azure:azure-messaging-webpubsub, version 1.5.0), a service-side client for managing WebSocket connections and broadcasting messages. A WebPubSubServiceClient is built via connection string, an access key (AzureKeyCredential plus endpoint), or DefaultAzureCredential, scoped to a specific hub (a logical isolation unit for connections), with an async variant also available. Key concepts are hub, group (a subset of connections within a hub), connection (an individual WebSocket client), and user (an entity that can own multiple connections). Sending covers broadcasting to all connections in a hub (sendToAll, plain text or JSON), broadcasting with an OData-style filter (by user ID exclusion, or by group membership combinations like "in GroupA and not in GroupB"), sending to a specific group (sendToGroup), a specific connection by ID (sendToConnection), or all connections belonging to a specific user (sendToUser). Group management covers adding/removing individual connections or entire users (all their connections) to/from a group, and checking whether a user belongs to a group. Connection management covers checking whether a connection or user is currently connected, closing a specific connection (optionally with a reason string), and bulk-closing all connections for a user or an entire group. Client access tokens are generated via getClientAccessToken, optionally scoped to a user ID, granted specific roles (e.g. webpubsub.joinLeaveGroup, webpubsub.sendToGroup, or their per-group variants), pre-joined to groups on connect, or given a custom expiration duration. Fine-grained permissions can also be granted, revoked, and checked directly on a connection (e.g. WebPubSubPermission.SEND_TO_GROUP scoped to a target group name). Async operations use reactive subscribe/doOnSuccess/doOnError patterns, and errors surface as HttpResponseException with status code and message. Best practices: organize connections into groups for targeted messaging, associate connections with user IDs for user-level messaging, set appropriate token expiration for security, grant only the minimal roles needed, use separate hubs to isolate different application features, and clean up inactive connections.
When to use - and when NOT to
Use this skill when building the service-side backend for a real-time WebSocket application - chat, live notifications, dashboards, or collaborative features - on Azure Web PubSub, needing to broadcast, target groups/users/connections, manage group membership, or issue scoped client access tokens. It is not the client-side WebSocket connection library (this SDK manages the service from the backend, issuing tokens that clients use to connect) and is not a general-purpose pub/sub message broker with persistence or replay - Web PubSub connections are ephemeral and message delivery is best-effort to currently connected clients. Roles also support a per-group form (e.g. webpubsub.sendToGroup.<group>) to scope a token's permission to one specific group rather than every group.
Inputs and outputs
Input is messages (text or JSON) plus a target scope (all connections, a filter, a group, a connection ID, or a user ID), or client access token parameters (user ID, roles, groups, expiration), with connection details supplied via WEB_PUBSUB_CONNECTION_STRING, WEB_PUBSUB_ENDPOINT, or WEB_PUBSUB_ACCESS_KEY. Output is delivery to matching live WebSocket connections, group-membership state changes, and generated client access tokens (with connection URL) that clients use to establish a WebSocket connection.
Integrations
Built on the Azure Web PubSub Java SDK (azure-messaging-webpubsub), authenticated via connection string, AzureKeyCredential, or DefaultAzureCredential, issuing access tokens consumed by WebSocket clients to connect to the service.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-webpubsub</artifactId>
<version>1.5.0</version>
</dependency>
Who it's for
Java developers building the backend for real-time WebSocket applications - chat, live notifications, dashboards - on Azure Web PubSub who need broadcast, group/user targeting, connection management, and scoped client access tokens, sending payloads as either WebPubSubContentType.TEXT_PLAIN or APPLICATION_JSON.
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.