Skip to content

Commit a93aac6

Browse files
committed
Add Java examples for message annotations in docs
- Included Java code snippets for creating, publishing, deleting, and subscribing to annotations. - Updated documentation to demonstrate usage of annotations API with Java examples.
1 parent 88859e7 commit a93aac6

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

src/pages/docs/messages/annotations.mdx

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,27 @@ await channel.annotations.publish(message.serial, {
228228
name: 'delivered'
229229
});
230230
```
231+
232+
```java
233+
// Create an Ably Realtime client specifying the clientId that will
234+
// be associated with annotations published by this client
235+
ClientOptions options = new ClientOptions("{{API_KEY}}");
236+
options.clientId = "my-client-id";
237+
AblyRealtime realtime = new AblyRealtime(options);
238+
239+
// Create a channel in a namespace called `annotations`
240+
// which has message annotations enabled
241+
Channel channel = realtime.channels.get("annotations:example");
242+
243+
// Publish an annotation for a message that flags it as delivered
244+
Annotation annotation = new Annotation();
245+
annotation.type = "receipts:flag.v1";
246+
annotation.name = "delivered";
247+
channel.annotations.publish(message, annotation);
248+
249+
// You can also use a message's `serial`
250+
channel.annotations.publish(message.serial, annotation);
251+
```
231252
</Code>
232253

233254
In the case of the `distinct`, `unique`, or `multiple` aggregation types, you should also specify a `name`. For these types, each different name will be aggregated separately in the [annotation summary](#annotation-summaries).
@@ -250,6 +271,15 @@ await channel.annotations.publish(message.serial, {
250271
count: 4
251272
});
252273
```
274+
275+
```java
276+
Annotation annotation = new Annotation();
277+
annotation.type = "rating:multiple.v1";
278+
annotation.name = "stars";
279+
annotation.count = 4;
280+
281+
channel.annotations.publish(message.serial, annotation);
282+
```
253283
</Code>
254284

255285
You can additionally specify a `data` payload when publishing an annotation. This is not included in an annotation summary, so only readable by [clients that are subscribed to individual annotation events](#individual-annotations).
@@ -296,6 +326,24 @@ await channel.annotations.delete(message.serial, {
296326
name: 'delivered'
297327
});
298328
```
329+
330+
```java
331+
// Create an Ably Realtime client specifying the clientId that will
332+
// be associated with annotations published by this client
333+
ClientOptions options = new ClientOptions("{{API_KEY}}");
334+
options.clientId = "my-client-id";
335+
AblyRealtime realtime = new AblyRealtime(options);
336+
337+
// Create a channel in a namespace called `annotations`
338+
// which has message annotations enabled
339+
Channel channel = realtime.channels.get("annotations:example");
340+
341+
// Delete a 'delivered' annotation
342+
Annotation annotation = new Annotation();
343+
annotation.type = "receipts:flag.v1";
344+
annotation.name = "delivered";
345+
channel.annotations.delete(message.serial, annotation);
346+
```
299347
</Code>
300348

301349
## Subscribe to annotation summaries <a id="subscribe" />
@@ -342,6 +390,24 @@ await channel.subscribe((message) => {
342390
}
343391
});
344392
```
393+
394+
```java
395+
// Create an Ably Realtime client specifying the clientId that will
396+
// be associated with annotations published by this client
397+
ClientOptions options = new ClientOptions("{{API_KEY}}");
398+
options.clientId = "my-client-id";
399+
AblyRealtime realtime = new AblyRealtime(options);
400+
401+
// Create a channel in a namespace called `annotations`
402+
// which has message annotations enabled
403+
Channel channel = realtime.channels.get("annotations:example");
404+
405+
channel.subscribe(message -> {
406+
if (message.action == MessageAction.MESSAGE_SUMMARY) {
407+
System.out.println(message.annotations.summary);
408+
}
409+
});
410+
```
345411
</Code>
346412

347413
### Annotation summaries <a id="annotation-summaries"/>
@@ -474,6 +540,30 @@ await channel.annotations.subscribe((annotation) => {
474540
}
475541
});
476542
```
543+
544+
```java
545+
// Create an Ably Realtime client specifying the clientId that will
546+
// be associated with annotations published by this client
547+
ClientOptions options = new ClientOptions("{{API_KEY}}");
548+
options.clientId = "my-client-id";
549+
AblyRealtime realtime = new AblyRealtime(options);
550+
551+
// Create a channel in a namespace called `annotations`
552+
// which has message annotations enabled
553+
Channel channel = realtime.channels.get("annotations:example");
554+
555+
channel.annotations.subscribe(annotation -> {
556+
if (annotation.action == AnnotationAction.ANNOTATION_CREATE) {
557+
System.out.println(
558+
String.format("New %s annotation with name %s from %s", annotation.type, annotation.name, annotation.clientId)
559+
);
560+
} else if (annotation.action == AnnotationAction.ANNOTATION_DELETE) {
561+
System.out.println(
562+
String.format("%s deleted a %s annotation with name %s", annotation.clientId, annotation.type, annotation.name)
563+
);
564+
}
565+
});
566+
```
477567
</Code>
478568

479569
### Annotation message properties <a id="annotation-properties"/>

0 commit comments

Comments
 (0)