Skip to content

Commit 0e22967

Browse files
committed
Add chat message.with() example
1 parent cc19bb8 commit 0e22967

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

content/chat/rooms/messages.textile

+76-2
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ The following are the properties of a message:
8080

8181
|_. Property |_. Description |_. Type |
8282
| serial | An Ably-generated ID used to uniquely identify the message. By comparing it to others it provides a deterministic global ordering of messages. | String |
83+
| 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 |
84+
| action | The latest action performed on this message, such as @message.create@, @message.update@ or @message.delete@. | String |
8385
| clientId | The client identifier of the user that created the message. | String |
8486
| roomId | The name of the room the message was created in. | String |
8587
| text | The message contents. | String |
8688
| 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 |
8789
| 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 |
8890
| createdAt | The time the message was created. | Date |
89-
| 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 |
9191
| timestamp | The time the action was performed. It will be identical to @createdAt@ if the action is a @message.create@. | Date |
9292
| operation | For updates and deletions, this provides additional details about the action. It may contain the following properties: | Object or undefined |
9393
| | clientId: The client identifier of the user associated with the action. | String or undefined |
@@ -570,3 +570,77 @@ Applying an action to a message produces a new version, which is uniquely identi
570570
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.
571571

572572
Update and Delete events provide the full message payload, so may be used to replace the entire earlier version of the message.
573+
574+
h2(#keep-messages-updated). Keep messages updated using @with()@
575+
576+
The "@Message@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Message.html object has a convenience 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. 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. This can be used to ensure that the latest version of a message is always obtained without manually comparing versions.
577+
578+
579+
Example usage to keep a list of messages updated:
580+
581+
```[javascript]
582+
import { MessageEvents, Message } from '@ably/chat';
583+
let myMessageList: Message[];
584+
585+
const { unsubscribe } = room.messages.subscribe((event) => {
586+
switch (event.type) {
587+
case MessageEvents.Created:
588+
myMessageList.push(event.message);
589+
break;
590+
case MessageEvents.Updated:
591+
case MessageEvents.Deleted:
592+
const idx = myMessageList.findIndex((msg) => msg.serial === event.message.serial);
593+
if (idx !== -1) {
594+
myMessageList[idx] = myMessageList[idx].with(event);
595+
}
596+
break;
597+
default:
598+
break;
599+
}
600+
});
601+
```
602+
603+
```[react]
604+
import { MessageEvents, useMessages, Message } from '@ably/chat';
605+
606+
const MyComponent = () => {
607+
// we use {list: []} to avoid copying the full array with every change
608+
// but still take advantage of React's state change detection
609+
const [ messages, setMessages ] = useState<{list: Message[]}>({list: []});
610+
useMessages({
611+
listener: (event) => {
612+
switch (event.type) {
613+
case MessageEvents.Created:
614+
setMessages((prev) => {
615+
// append new message
616+
prev.list.push(event.message);
617+
// update reference without copying whole array
618+
return { list: prev.list };
619+
});
620+
break;
621+
case MessageEvents.Updated:
622+
case MessageEvents.Deleted:
623+
setMyMessageList((prev) => {
624+
// find existing message to apply update or delete to
625+
const existing = prev.list.findIndex((msg) => msg.serial === event.message.serial);
626+
if (existing === -1) {
627+
return prev; // no change if not found
628+
}
629+
const newMsg = existing.with(event);
630+
if (newMsg === existing) {
631+
// with() returns the same object if the event is older,
632+
// so in this case no change is needed
633+
return prev;
634+
}
635+
// set new message and update reference without copying whole array
636+
prev.list[existing] = newMsg;
637+
return { list: prev.list };
638+
});
639+
break;
640+
}
641+
},
642+
});
643+
644+
return <div>...</div>;
645+
};
646+
```

0 commit comments

Comments
 (0)