title | description | author | ms.author | ms.service | ms.custom | ms.topic | ms.date |
---|---|---|---|---|---|---|---|
Reference - Java SDK for Azure Web PubSub |
This reference describes the Java SDK for the Azure Web PubSub service. |
vicancy |
lianwei |
azure-web-pubsub |
devx-track-extended-java |
conceptual |
01/31/2023 |
Azure Web PubSub service is an Azure-managed service that helps developers easily build web applications with real-time features and a publish-subscribe pattern. Any scenario that requires real-time publish-subscribe messaging between server and clients or among clients can use Azure Web PubSub service. Traditional real-time features that often require polling from the server or submitting HTTP requests can also use Azure Web PubSub service.
This article describes the Azure Web PubSub service client library.
You can use this library in your server-side app to manage the WebSocket client connections, as shown in this diagram:
Use this library to:
- Send messages to hubs and groups.
- Send messages to particular users and connections.
- Organize users and connections into groups.
- Close connections
- Grant, revoke, and check permissions for an existing connection
For more information, see:
- Azure Web PubSub client library Java SDK
- Azure Web PubSub client library reference documentation
- Azure Web PubSub client library samples for Java
- Azure Web PubSub service documentation
- An Azure account with an active subscription is required. If you don't already have one, you can create an account for free.
- A Java Development Kit (JDK), version 8 or later.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-webpubsub</artifactId>
<version>1.0.0</version>
</dependency>
WebPubSubServiceClient webPubSubServiceClient = new WebPubSubServiceClientBuilder()
.connectionString("{connection-string}")
.hub("chat")
.buildClient();
WebPubSubServiceClient webPubSubServiceClient = new WebPubSubServiceClientBuilder()
.credential(new AzureKeyCredential("{access-key}"))
.endpoint("<Insert endpoint from Azure Portal>")
.hub("chat")
.buildClient();
- Broadcast message to entire hub
- Broadcast message to a group
- Send message to a connection
- Send message to a user
webPubSubServiceClient.sendToAll("Hello world!", WebPubSubContentType.TEXT_PLAIN);
webPubSubServiceClient.sendToGroup("java", "Hello Java!", WebPubSubContentType.TEXT_PLAIN);
webPubSubServiceClient.sendToConnection("myconnectionid", "Hello connection!", WebPubSubContentType.TEXT_PLAIN);
webPubSubServiceClient.sendToUser("Andy", "Hello Andy!", WebPubSubContentType.TEXT_PLAIN);
You can set the AZURE_LOG_LEVEL
environment variable to view logging statements made in the client library. For
example, setting AZURE_LOG_LEVEL=2
would show all informational, warning, and error log messages. The log levels can
be found here: log levels.
All client libraries use the Netty HTTP client by default. Adding the above dependency will automatically configure the client library to use the Netty HTTP client. Configuring or changing the HTTP client is described in the HTTP clients wiki.
By default, all client libraries use the Tomcat-native Boring SSL library to enable native-level performance for SSL operations. The Boring SSL library is an uber jar containing native libraries for Linux / macOS / Windows, and provides better performance compared to the default SSL implementation within the JDK. For more information, including how to reduce the dependency size, see [performance tuning][https://github.com/Azure/azure-sdk-for-java/wiki/Performance-Tuning].
[!INCLUDE next step]