You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/pages/docs/chat/rooms/messages.mdx
+47-41Lines changed: 47 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,11 +67,12 @@ The following is the structure of a message:
67
67
"text": "What a shot!",
68
68
"headers": {},
69
69
"metadata": {},
70
-
"createdAt": new Date("2024-06-12T11:37:59.988Z"),
71
-
"action": "message.create",
72
-
"version": "01826232498871-001@abcdefghij:001",
73
70
"timestamp": new Date("2024-06-12T11:37:59.988Z"),
74
-
"operation": {},
71
+
"action": "message.create",
72
+
"version": {
73
+
"serial": "01826232498871-001@abcdefghij:001",
74
+
"timestamp": new Date("2024-06-12T11:37:59.988Z")
75
+
}
75
76
}
76
77
```
77
78
</Code>
@@ -85,14 +86,14 @@ The following are the properties of a message:
85
86
| text | The message contents. | String |
86
87
| headers | Optional headers for adding additional information to a message, such as the relative timestamp of a livestream video, or flagging a message as important. Do not use the headers for authoritative information. There is no server-side validation. When reading headers treat them like user input. | Object |
87
88
| metadata | Optional additional metadata about the message, such as animations, effects or links to other resources such as images. This information is not read by Ably. Do not use metadata for authoritative information. There is no server-side validation. When reading metadata treat it like user input. | Object |
88
-
|createdAt| The time the message was created. | Date |
89
+
|timestamp| The time the message was created. | Date |
89
90
| action | The latest action performed on this message, such as `message.create`, `message.update` or `message.delete`. | String |
90
-
| version |An Ably-generated ID used to uniquely identify the version of the message. It provides a deterministic global ordering of message versions. The `version` is identical to `serial` if the action is `message.create`. | String|
91
-
|timestamp | The time the action was performed. It will be identical to `createdAt` if the action is a `message.create`. |Date|
92
-
|operation | For updates and deletions, this provides additional details about the action. It may contain the following properties: | Object or undefined|
93
-
||`clientId`: The client identifier of the user associated with the action. | String or undefined |
94
-
||`description`: Optional description for the action. | String or undefined |
95
-
||`metadata`: Optional additional metadata about the action. | Object or undefined |
91
+
| version |Contains information about the current version of the message. For `message.create` actions, only `serial` and `timestamp` are set. For `message.update` and `message.delete` actions, additional fields are included. | Object|
92
+
||`serial`: An Ably-generated ID used to uniquely identify the version of the message. It provides a deterministic global ordering of message versions. The `version.serial` is identical to `serial` if the action is `message.create`. |String|
93
+
||`timestamp`: The time the action was performed. It will be identical to `timestamp` if the action is a `message.create`. | Date|
94
+
||`clientId`: The client identifier of the user that created this version of the message. Only set for `message.update` and `message.delete` actions. | String or undefined |
95
+
||`description`: Optional description provided by the client that created this message version. Only set for `message.update` and `message.delete` actions. | String or undefined |
96
+
||`metadata`: Optional description provided by the client that created this message version. Only set for `message.update` and `message.delete` actions. | Object or undefined |
96
97
97
98
See [below](#global-ordering) for more information on how to apply deterministic global ordering to the chat messages in your application.
98
99
@@ -390,27 +391,30 @@ The following is the structure of an updated message:
390
391
"text": "What a shot! Edit: I meant to say 'What a dunk!'",
391
392
"headers": {},
392
393
"metadata": {},
393
-
"createdAt": new Date("2024-06-12T11:37:59.988Z")S,
394
+
"timestamp": new Date("2024-06-12T11:37:59.988Z"),
394
395
"action": "message.update",
395
-
"version": "01826232498871-001@abcdefghij:001",
396
-
"timestamp": new Date("2024-11-21T15:49:25.425Z"),
397
-
"operation": {
396
+
"version": {
397
+
"serial": "01826232498871-001@abcdefghij:001",
398
+
"timestamp": new Date("2024-11-21T15:49:25.425Z"),
398
399
"clientId": "basketLover014",
399
400
"description": "Message updated by client",
400
401
"metadata": {}
401
-
},
402
+
}
402
403
}
403
404
```
404
405
</Code>
405
406
406
407
The updated message response is identical to the structure of a message, with the following differences:
407
408
408
-
| Property | Description |
409
-
| -------- | ----------- |
410
-
| action | Set to `message.update`. |
411
-
| version | Set to the serial of the update action. |
412
-
| timestamp | Set to the time the message was updated. |
413
-
| operation | Set to the details the actioning client provided in the request. |
409
+
| Property | Description | Type |
410
+
| -------- | ----------- | ---- |
411
+
| action | Set to `message.update`. | string |
412
+
| version | Contains additional fields compared to `message.create` action: | object |
413
+
||`serial`: Set to the serial of the update action. | string |
414
+
||`timestamp`: Set to the time the message was updated. | Date |
415
+
||`clientId`: The client identifier of the user who performed the update. | String or undefined |
416
+
||`description`: Optional description provided in the update request. | String or undefined |
417
+
||`metadata`: Optional metadata provided in the update request. | Object or undefined |
414
418
415
419
## Delete a message <aid="delete"/>
416
420
@@ -489,7 +493,6 @@ Use the [`useMessages`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typed
Chat messages and update events are delivered in realtime to clients connected to a particular region in the order in which that region receives them. The order in which a given region receives these events may be different from the "global" order of events, i.e. the true time-based order in which events happened.
617
623
618
-
Chat messages are uniquely identified by their `serial` and may have multiple `versions` as a result of edit and delete operations. Both `serial` and `version` are lexicographically sortable strings. This means they can be used to enforce a deterministic global ordering based on string comparison.
624
+
Chat messages are uniquely identified by their `serial` and may have multiple `versions` as a result of edit and delete operations. Both `serial` and `version.serial` are lexicographically sortable strings. This means they can be used to enforce a deterministic global ordering based on string comparison.
619
625
620
626
### Ordering new messages <aid="ordering-new"/>
621
627
@@ -625,15 +631,15 @@ The `Message` object also has convenience methods [`before`](https://sdk.ably.co
625
631
626
632
### Ordering updates and deletes <aid="ordering-update-delete"/>
627
633
628
-
Applying an action to a message produces a new version, which is uniquely identified by the `version` property. When two message instances share the same `serial` they represent the same chat message, but they can represent different versions. Lexicographically sorting the two message instances by the `version` property gives the global order of the message versions: the message instance with a greater `version` is newer, the message instance with a lower `version` is older, and if their `version` is equal then they are the same version.
634
+
Applying an action to a message produces a new version, which is uniquely identified by the `version.serial` property. When two message instances share the same `serial` they represent the same chat message, but they can represent different versions. Lexicographically sorting the two message instances by the `version.serial` property gives the global order of the message versions: the message instance with a greater `version.serial` is newer, the message instance with a lower `version.serial` is older, and if their `version.serial` is equal then they are the same version.
629
635
630
-
The `Message` object also has convenience methods [`isOlderVersionOf`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Message.html#isolderversionof), [`isNewerVersionOf`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Message.html#isnewerversionof) and [`isSameVersionAs`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Message.html#issameversionas) which provide the same comparison.
636
+
The `Message` object also has convenience methods [`isOlderVersionOf`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Message.html#isolderversionof), [`isNewerVersionOf`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Message.html#isnewerversionof) and [`isSameVersionAs`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Message.html#issameversionas) which provide the same comparison by comparing the `version.serial` values internally.
631
637
632
638
Update and Delete events provide the message payload without message reactions. To correctly use message reactions, always use the [`with()`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Message.html#with) method to apply the event to the message instance.
633
639
634
640
## Keep messages updated using with() <aid="keep-messages-updated"/>
635
641
636
-
The [`Message`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Message.html) object has a method [`with`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Message.html#with) that takes a [`MessageEvent`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.MessageEvent.html), automatically compares versions, and returns the newest `Message` instance. For updates and deletes, if `with` is called with an event that is older than the message, the message is returned. If it is called with a newer event, the message from the event is returned. For message reaction events, the reactions will be correctly applied to the returned message.
642
+
The [`Message`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Message.html) object has a method [`with`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Message.html#with) that takes a [`MessageEvent`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.MessageEvent.html), automatically compares version serials, and returns the newest `Message` instance. For updates and deletes, if `message.with(event)` is called with an `event` that has an older `version.serial`than the `message`, then the `message` is returned unchanged. If it is called with a newer event (greater `version.serial`), then the message from the event is returned. For message reaction events, the reactions will be correctly applied to the returned message.
637
643
638
644
`Message.with()` also ensures that reactions from existing messages are copied over to the new message instance in the case of UPDATEs or DELETEs.
0 commit comments