Skip to content

Commit 5bcc3c4

Browse files
committed
Add swift code examples.
1 parent a93aac6 commit 5bcc3c4

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

src/pages/docs/messages/annotations.mdx

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,37 @@ channel.annotations.publish(message, annotation);
249249
// You can also use a message's `serial`
250250
channel.annotations.publish(message.serial, annotation);
251251
```
252+
253+
```swift
254+
// Create an Ably Realtime client specifying the clientId that will
255+
// be associated with annotations published by this client
256+
let options = ARTClientOptions(key: "{{API_KEY}}")
257+
options.clientId = "my-client-id"
258+
let realtime = ARTRealtime(options: options)
259+
260+
// Create a channel in a namespace called `annotations`
261+
// which has message annotations enabled
262+
let channel = realtime.channels.get("annotations:example")
263+
264+
// Publish an annotation for a message that flags it as delivered
265+
let annotation = ARTOutboundAnnotation(
266+
id: nil,
267+
type: "receipts:flag.v1",
268+
clientId: nil,
269+
name: "delivered",
270+
count: nil,
271+
data: nil,
272+
extras: nil
273+
)
274+
channel.annotations.publish(for: message, annotation: annotation) { error in
275+
// Handle error if needed
276+
}
277+
278+
// You can also use a message's `serial`
279+
channel.annotations.publish(forMessageSerial: messageSerial, annotation: annotation) { error in
280+
// Handle error if needed
281+
}
282+
```
252283
</Code>
253284

254285
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).
@@ -280,6 +311,22 @@ annotation.count = 4;
280311

281312
channel.annotations.publish(message.serial, annotation);
282313
```
314+
315+
```swift
316+
let annotation = ARTOutboundAnnotation(
317+
id: nil,
318+
type: "rating:multiple.v1",
319+
clientId: nil,
320+
name: "stars",
321+
count: 4,
322+
data: nil,
323+
extras: nil
324+
)
325+
326+
channel.annotations.publish(forMessageSerial: message.serial, annotation: annotation) { error in
327+
// Handle error if needed
328+
}
329+
```
283330
</Code>
284331

285332
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).
@@ -344,6 +391,32 @@ annotation.type = "receipts:flag.v1";
344391
annotation.name = "delivered";
345392
channel.annotations.delete(message.serial, annotation);
346393
```
394+
395+
```swift
396+
// Create an Ably Realtime client specifying the clientId that will
397+
// be associated with annotations published by this client
398+
let options = ARTClientOptions(key: "{{API_KEY}}")
399+
options.clientId = "my-client-id"
400+
let realtime = ARTRealtime(options: options)
401+
402+
// Create a channel in a namespace called `annotations`
403+
// which has message annotations enabled
404+
let channel = realtime.channels.get("annotations:example")
405+
406+
// Delete a 'delivered' annotation
407+
let annotation = ARTOutboundAnnotation(
408+
id: nil,
409+
type: "receipts:flag.v1",
410+
clientId: nil,
411+
name: "delivered",
412+
count: nil,
413+
data: nil,
414+
extras: nil
415+
)
416+
channel.annotations.delete(forMessageSerial: message.serial, annotation: annotation) { error in
417+
// Handle error if needed
418+
}
419+
```
347420
</Code>
348421

349422
## Subscribe to annotation summaries <a id="subscribe" />
@@ -408,6 +481,24 @@ channel.subscribe(message -> {
408481
}
409482
});
410483
```
484+
485+
```swift
486+
// Create an Ably Realtime client specifying the clientId that will
487+
// be associated with annotations published by this client
488+
let options = ARTClientOptions(key: "{{API_KEY}}")
489+
options.clientId = "my-client-id"
490+
let realtime = ARTRealtime(options: options)
491+
492+
// Create a channel in a namespace called `annotations`
493+
// which has message annotations enabled
494+
let channel = realtime.channels.get("annotations:example")
495+
496+
channel.subscribe { message in
497+
if message.action == .messageSummary {
498+
print(message.annotations?.summary ?? [:])
499+
}
500+
}
501+
```
411502
</Code>
412503

413504
### Annotation summaries <a id="annotation-summaries"/>
@@ -564,6 +655,26 @@ channel.annotations.subscribe(annotation -> {
564655
}
565656
});
566657
```
658+
659+
```swift
660+
// Create an Ably Realtime client specifying the clientId that will
661+
// be associated with annotations published by this client
662+
let options = ARTClientOptions(key: "{{API_KEY}}")
663+
options.clientId = "my-client-id"
664+
let realtime = ARTRealtime(options: options)
665+
666+
// Create a channel in a namespace called `annotations`
667+
// which has message annotations enabled
668+
let channel = realtime.channels.get("annotations:example")
669+
670+
channel.annotations.subscribe { annotation in
671+
if annotation.action == .create {
672+
print("New \(annotation.type) annotation with name \(annotation.name ?? "") from \(annotation.clientId ?? "")")
673+
} else if annotation.action == .delete {
674+
print("\(annotation.clientId ?? "") deleted a \(annotation.type) annotation with name \(annotation.name ?? "")")
675+
}
676+
}
677+
```
567678
</Code>
568679

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

0 commit comments

Comments
 (0)