-
Notifications
You must be signed in to change notification settings - Fork 44
Chat: A how to guide for replies #2712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
splindsay-92
wants to merge
8
commits into
main
Choose a base branch
from
chat/how-to-replies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e41f2ab
Chat: Add guide for implementing replies and quotes in TypeScript
splindsay-92 5cb9c32
Chat: Add anchor links for sections and code tags
splindsay-92 e0dde2e
Chat: wrap react code examples in javascript tag so they display.
splindsay-92 c88a7d4
Add typescript lang support and hide the react tag and languages list…
splindsay-92 2a6cab0
Refactor based on PR comments
splindsay-92 1104466
Moved replies to chat rooms section.
splindsay-92 ee12076
Refactor replies documentation based on comments
splindsay-92 944ab31
Refactor: Overhaul guide to follow flow and style of the media.mdx gu…
splindsay-92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export const languageKeys = [ | ||
'javascript', | ||
'typescript', | ||
'react', | ||
'java', | ||
'ruby', | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
--- | ||
title: "Implement replies" | ||
meta_description: "Add reply functionality to messages in a chat room." | ||
meta_keywords: "ably chat, message replies, chat replies, javascript chat replies, typescript chat replies, chat metadata" | ||
--- | ||
|
||
Implement replies by storing reference information in message metadata when users respond to other messages. | ||
|
||
Use the [metadata](/docs/chat/rooms/messages#structure) field to store reply information when [sending a message](/docs/chat/rooms/messages#send). Use the [get()](/docs/chat/rooms/messages#get-by-serial) method to fetch individual parent messages by serial, and [subscribe](/docs/chat/rooms/messages#subscribe) to receive new messages including replies in the room. | ||
|
||
## Message fields | ||
|
||
Use these message fields to implement replies: | ||
- `serial`: Unique message identifier | ||
- `clientId`: Sender ID | ||
- `text`: Message content | ||
- `createdAt`: Timestamp | ||
- `metadata`: Custom data storage | ||
|
||
## Store reply information <a id="store-information"/> | ||
|
||
When a user replies to a message, extract and store the parent message details: | ||
|
||
<Code> | ||
```javascript | ||
function prepareReply(parentMessage) { | ||
return { | ||
serial: parentMessage.serial, | ||
createdAt: parentMessage.createdAt.getTime(), | ||
clientId: parentMessage.clientId, | ||
previewText: parentMessage.text.substring(0, 140) | ||
}; | ||
} | ||
``` | ||
|
||
```react | ||
const prepareReply = (parentMessage) => { | ||
return { | ||
serial: parentMessage.serial, | ||
createdAt: parentMessage.createdAt.getTime(), | ||
clientId: parentMessage.clientId, | ||
previewText: parentMessage.text.substring(0, 140) | ||
}; | ||
}; | ||
``` | ||
</Code> | ||
|
||
## Send reply <a id="send-reply"/> | ||
|
||
Include the parent message information in the metadata when sending: | ||
|
||
<Code> | ||
```javascript | ||
async function sendReply(replyToMessage, replyText) { | ||
const metadata = { | ||
reply: { | ||
serial: replyToMessage.serial, | ||
timestamp: replyToMessage.createdAt.getTime(), | ||
clientId: replyToMessage.clientId, | ||
previewText: replyToMessage.text.substring(0, 140) | ||
} | ||
}; | ||
|
||
await room.messages.send({ | ||
text: replyText, | ||
metadata: metadata | ||
}); | ||
} | ||
``` | ||
|
||
```react | ||
import { useMessages } from '@ably/chat/react'; | ||
|
||
const ReplyComponent = ({ messageToReplyTo }) => { | ||
const { sendMessage } = useMessages(); | ||
|
||
const sendReply = async (replyText) => { | ||
const metadata = { | ||
reply: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, use the helper defined earlier? |
||
serial: messageToReplyTo.serial, | ||
createdAt: messageToReplyTo.createdAt.getTime(), | ||
clientId: messageToReplyTo.clientId, | ||
previewText: messageToReplyTo.text.substring(0, 140) | ||
} | ||
}; | ||
|
||
await sendMessage({ | ||
text: replyText, | ||
metadata: metadata | ||
}); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button onClick={() => sendReply("My reply")}>Send Reply</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
</Code> | ||
|
||
## Display replies <a id="handle-received-messages"/> | ||
|
||
Check incoming messages for reply metadata and display accordingly: | ||
|
||
<Code> | ||
```javascript | ||
room.messages.subscribe((messageEvent) => { | ||
const message = messageEvent.message; | ||
|
||
if (message.metadata?.reply) { | ||
const replyData = message.metadata.reply; | ||
const parentMessage = localMessages.find(msg => msg.serial === replyData.serial); | ||
|
||
if (parentMessage) { | ||
console.log(`Reply to ${parentMessage.clientId}: ${parentMessage.text}`); | ||
} else { | ||
console.log(`Reply to ${replyData.clientId}: ${replyData.previewText}`); | ||
} | ||
} | ||
|
||
console.log(`Message: ${message.text}`); | ||
}); | ||
``` | ||
|
||
```react | ||
import { useMessages } from '@ably/chat/react'; | ||
import { ChatMessageEventType } from '@ably/chat'; | ||
|
||
const MessageList = () => { | ||
const [messages, setMessages] = useState([]); | ||
|
||
useMessages({ | ||
listener: (event) => { | ||
if (event.type === ChatMessageEventType.Created) { | ||
setMessages(prev => [...prev, event.message]); | ||
} | ||
} | ||
}); | ||
|
||
const findParentMessage = (replyData) => { | ||
return messages.find(msg => msg.serial === replyData.serial); | ||
}; | ||
|
||
return ( | ||
<div> | ||
{messages.map(message => ( | ||
<div key={message.serial}> | ||
{message.metadata?.reply && ( | ||
<div> | ||
Replying to: {message.metadata.reply.previewText} | ||
</div> | ||
)} | ||
<div>{message.text}</div> | ||
</div> | ||
))} | ||
</div> | ||
); | ||
}; | ||
``` | ||
</Code> | ||
|
||
## Fetch missing parent messages <a id="find-parent-messages"/> | ||
|
||
If a parent message isn't in local state, fetch it directly using its serial: | ||
|
||
<Code> | ||
```javascript | ||
async function fetchParentMessage(replyData) { | ||
const message = await room.messages.get(replyData.serial); | ||
return message; | ||
} | ||
``` | ||
|
||
```react | ||
const FetchParentMessage = ({ replyData }) => { | ||
const [parentMessage, setParentMessage] = useState(); | ||
|
||
useEffect(() => { | ||
const fetchMessage = async () => { | ||
const message = await room.messages.get(replyData.serial); | ||
setParentMessage(message); | ||
}; | ||
|
||
fetchMessage(); | ||
}, [replyData]); | ||
|
||
return parentMessage ? ( | ||
<div>{parentMessage.text}</div> | ||
) : null; | ||
}; | ||
``` | ||
</Code> | ||
|
||
## Considerations | ||
|
||
Keep in mind that: | ||
- Old messages may not be available depending on message persistence settings. | ||
- Messages can be updated, potentially removing reply references. | ||
- Metadata is not server-validated. | ||
- Nested replies can be complex and expensive to implement, so consider limiting reply depth. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
prepareReply
here?