Read receipts
Read receipts indicate if other channel members have received and viewed a message.
Required setup
Read Receipts feature is tightly coupled with the Unread Message Count feature. To receive message read receipts, you must know which message was last read by which user on a given channel. That's why, to implement the Read Receipts feature, you must first set the timetoken of the last message a user read on a given channel. Based on that, Chat SDK will map a user's last read message to a given message timetoken and let you show this mapping result in your chat app as read or unread.
Get read receipts
streamReadReceipts()
lets you get a read confirmation status for messages you published on a channel.
When called, the method fetches the read status of the members, listens for new messages, and updates the read status accordingly. The read status is then passed as an event of the Receipt
type to a an asynchronous stream for further processing or display.
Not available for public chats
Read receipts are disabled in public chats. If you try implementing this feature in a public channel type, you'll get the Read receipts are not supported in Public chats
error.
Method signature
channel.streamReadReceipts() -> AsyncStream<[Timetoken: [String]]>
Input
This method doesn't take any parameters.
Output
Parameter | Description |
---|---|
AsyncStream<[Timetoken: [String]]> | An asynchronous stream that emits a new read receipt confirmation whenever someone marks a message as read. |
Basic Usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Receive updates for read receipts on the support
channel.
- AsyncStream
- Closure
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
for await readReceipt in channel.streamReadReceipts() {
readReceipt.forEach { (messageTimetoken, users) in
debugPrint("Message timetoken: \(messageTimetoken) was read by users: \(users)")
}
}
} else {
debugPrint("Channel not found")
}
}
// Assuming you have a reference of type "ChannelImpl" named "channel"
autoCloseable = channel.streamReadReceipts { receipts in
print("Read Receipts Received:")
receipts.forEach { (messageTimetoken, users) in
print("Message Timetoken: \(messageTimetoken) was read by users: \(users)")
}
}