|
1 |
| -# Using message annotations with Pub/Sub |
| 1 | +# Adding annotations to messages with Pub/Sub |
2 | 2 |
|
3 |
| -This example demonstrates how to use Ably's message annotations feature to add additional metadata to messages in a pub/sub system. Message annotations allow clients to add reactions, flags, or other metadata to messages without modifying the original message content. |
| 3 | +Enable users to annotate messages with additional data, such as reactions, flags, or other contextual information without modifying the original message content. |
| 4 | + |
| 5 | +Message annotations provide a powerful way to extend messages with additional information. Unlike editing a message, annotations allow multiple clients to add their own metadata while preserving the original message. This is ideal for implementing features like reactions, content categorization, moderation flags, or any other metadata that enhances message context. |
| 6 | + |
| 7 | +Message annotations are implemented using [Ably Pub/Sub](/docs/channels). The Pub/Sub SDK with annotations provides a way to add structured metadata to messages, with support for different annotation types and automatic summarization. |
4 | 8 |
|
5 | 9 | ## Resources
|
6 | 10 |
|
7 |
| -- [Ably Documentation](https://ably.com/docs) |
8 |
| -- [Message Annotations Documentation](https://ably.com/docs/messages/annotations) |
| 11 | +Use the following methods to work with message annotations in a pub/sub application: |
9 | 12 |
|
10 |
| -The channel name must be in a mutable message namespace, which is why we're using `mutable:pub-sub-message-annotations` in this example. |
| 13 | +- [`channels.get()`](/docs/channels#create) - creates a new or retrieves an existing `channel`. Specify the `ANNOTATION_PUBLISH` and `ANNOTATION_SUBSCRIBE` modes to publish and subscribe to message annotations. |
| 14 | +- [`channel.subscribe()`](/docs/pub-sub#subscribe) - subscribes to message events within a specific channel by registering a listener. Message events with a `message.create` action are received when a user publishes a message. Message events with a `message.summary` action are received when a user publishes or deletes an annotation. |
| 15 | +<!-- TODO links --> |
| 16 | +- `channel.annotations.publish()` - publishes an annotation for a specific message |
| 17 | +- `channel.annotations.subscribe()` - subscribes to receive individual annotation events |
| 18 | +- `channel.annotations.delete()` - deletes a previously published annotation |
11 | 19 |
|
12 |
| -## Features |
| 20 | +<!-- TODO link --> |
| 21 | +Find out more about annotations. |
13 | 22 |
|
14 |
| -In this example, you can: |
| 23 | +## Annotation Types |
15 | 24 |
|
16 |
| -1. Publish messages to a channel |
17 |
| -2. Add annotations to messages using different annotation types: |
18 |
| - - **distinct.v1**: Track different values with client IDs that created them |
19 |
| - - **unique.v1**: Similar to distinct, but only the first client ID is recorded |
20 |
| - - **multiple.v1**: Track counts of values by different clients |
21 |
| - - **flag.v1**: Simple flags with client IDs |
22 |
| - - **total.v1**: Count the total annotations |
| 25 | +This example demonstrates five common annotation types, each suited to different use cases: |
23 | 26 |
|
24 |
| -3. View annotation summaries showing: |
25 |
| - - How many of each annotation type exist |
26 |
| - - Which clients contributed annotations |
27 |
| - - The values of annotations |
28 |
| - - Expandable/collapsible sections for detailed information |
| 27 | +<!-- TODO --> |
| 28 | + |
| 29 | +## Features |
| 30 | + |
| 31 | +This example demonstrates: |
| 32 | + |
| 33 | +1. Publishing regular messages to a channel |
| 34 | +2. Adding different types of annotations to messages |
| 35 | +3. Viewing both summarized and raw annotation data |
| 36 | +4. Deleting annotations |
29 | 37 |
|
30 | 38 | ## Getting started
|
31 | 39 |
|
@@ -63,16 +71,19 @@ In this example, you can:
|
63 | 71 |
|
64 | 72 | 7. Try it out by opening two tabs to [http://localhost:5173/](http://localhost:5173/) with your browser to see the result.
|
65 | 73 |
|
| 74 | +## Technical notes |
| 75 | + |
| 76 | +- Annotations require a channel in the mutable channel namespace. This example uses `mutable:pub-sub-message-annotations` |
| 77 | +- Annotations are logically grouped by an annotation namespace. This example uses `my-annotations` |
| 78 | + |
66 | 79 | ## How to use this example
|
67 | 80 |
|
68 |
| -1. Enter a message in the input field and click "Publish" to send it to the channel. |
69 |
| -2. After publishing a message, you'll see it appear in the message list with an expandable annotation interface. |
70 |
| -3. To add an annotation, select an annotation type from the dropdown menu, enter a value, and click "Publish". |
71 |
| -4. Open the example in multiple browser tabs with different URL parameters (e.g., `?clientId=user1` and `?clientId=user2`) to see how annotations from different clients are handled. |
72 |
| -5. Experiment with different annotation types: |
73 |
| - - For reactions, try using the "multiple.v1" type with values like "👍" or "❤️" |
74 |
| - - For flagging content, use the "flag.v1" type |
75 |
| - - For tracking unique entries, use "distinct.v1" or "unique.v1" |
| 81 | +1. Enter a message in the input field and click "Publish" to send it to the channel |
| 82 | +2. Click on a message to expand it and reveal the annotation interface |
| 83 | +3. Select an annotation type, enter a value, and click "Publish" to add an annotation |
| 84 | +4. Switch between the "Summary" and "Raw Annotations" tabs to see different views |
| 85 | +5. Open the example in multiple browser tabs with different client IDs (e.g., `?clientId=user1` and `?clientId=user2`) to see how annotations from different clients are handled and summarized |
| 86 | +6. Delete annotations by clicking the trash icon in the raw annotations view and see how the summary is updated |
76 | 87 |
|
77 | 88 | ## Open in CodeSandbox
|
78 | 89 |
|
|
0 commit comments