用于 Ably 实时消息服务的 Java、Android、Clojure 和 Scala 客户端库 SDK
Build any realtime experience using Ably’s Pub/Sub Java SDK. Supported on all popular platforms and frameworks, including Kotlin and Android.
Ably Pub/Sub provides flexible APIs that deliver features such as pub-sub messaging, message history, presence, and push notifications. Utilizing Ably’s realtime messaging platform, applications benefit from its highly performant, reliable, and scalable infrastructure.
Find out more:
Everything you need to get started with Ably:
Ably aims to support a wide range of platforms. If you experience any compatibility issues, open an issue in the repository or contact Ably support.
The following platforms are supported:
| Platform | Support |
|---|---|
| Java | >= 1.8 (JRE 8 or later) |
| Kotlin | All versions (>= 1.0 supported), but we recommend >= 1.8 for best compatibility. |
| Android | >=4.4 (API level 19) |
[!IMPORTANT] SDK versions io.ably ably-java 1.8.2
### Install for Gradle:
```gradle
implementation 'io.ably:ably-java:1.8.2'
implementation 'org.slf4j:slf4j-simple:2.0.7'Run the following to instantiate a client:
import io.ably.lib.realtime.AblyRealtime;
import io.ably.lib.types.ClientOptions;
ClientOptions options = new ClientOptions(apiKey);
AblyRealtime realtime = new AblyRealtime(options);The following code connects to Ably's realtime messaging service, subscribes to a channel to receive messages, and publishes a test message to that same channel.
// Initialize Ably Realtime client
ClientOptions options = new ClientOptions("your-ably-api-key");
options.clientId = "me";
AblyRealtime realtimeClient = new AblyRealtime(options);
// Wait for connection to be established
realtimeClient.connection.on(ConnectionEvent.connected, connectionStateChange -> {
System.out.println("Connected to Ably");
// Get a reference to the 'test-channel' channel
Channel channel = realtimeClient.channels.get("test-channel");
// Subscribe to all messages published to this channel
channel.subscribe(message -> {
System.out.println("Received message: " + message.data);
});
// Publish a test message to the channel
channel.publish("test-event", "hello world");
});Ably Live Objects provide realtime, collaborative data structures that automatically synchronize state across all connected clients. Build interactive applications with shared data that updates instantly across devices.
For a comprehensive guide, check the Ably Live Objects documentation, starting with the Java quickstart.
Add the following dependency to your build.gradle file:
dependencies {
runtimeOnly("io.ably:liveobjects:1.8.2")
}
…
java
import io.ably.lib.realtime.AblyRealtime;
import io.ably.lib.rest.AblyRest;
import io.ably.lib.transport.Defaults;
import io.ably.lib.types.ClientOptions;
import io.ably.lib.types.ProxyOptions;
import io.ably.lib.http.HttpAuth;
public class AblyWithProxy {
public static void main(String[] args) throws Exception {
// Configure Ably Client options
ClientOptions options = new ClientOptions();
// Setup proxy settings
ProxyOptions proxy = new ProxyOptions();
proxy.host = "your-proxy-host"; // Replace with your proxy host
proxy.port = 8080; // Replace with your proxy port
// Optional: If the proxy requires authentication
proxy.username = "your-username"; // Replace with proxy username
proxy.password = "your-password"; // Replace with proxy password
proxy.prefAuthType = HttpAuth.Type.BASIC; // Choose your preferred authentication type (e.g., BASIC or DIGEST)
// Attach the proxy settings to the client options
options.proxy = proxy;
// Create an instance of Ably using the configured options
AblyRest ably = new AblyRest(options);
// Alternatively, for real-time connections
AblyRealtime ablyRealtime = new AblyRealtime(options);
// Use the Ably client as usual
}
}Read the CONTRIBUTING.md guidelines to contribute to Ably.
The CHANGELOG.md contains details of the latest releases for this SDK. You can also view all Ably releases on changelog.ably.com.
For help or technical support, visit Ably's support page or GitHub Issues for community-reported bugs and discussions.
暂无开放 Issues,或尚未同步最近议题。