Unread messages

Users can have unread messages in a channel because new messages arrived while they were away, or because they explictly marked a message as unread.

Marking a message as unread

You can mark a message as unread from client-side:

val channelClient = chatClient.channel(cid = "<channel_cid>")
channelClient.markUnread(messageId = "<message_id>").enqueue { result ->
    if (result is Result.Success) {
        // Message marked as unread
    } else {
        // Handle Result.Failure
    }
}

Or from server-side by providing a user id:

await channel.markUnread({ message_id: "<message_id>", user_id: "<user_id>" });

For performance reasons it’s only possible to mark any of the last 100 messages of the channel as unread.

Marking a channel as read

You can mark all messages in a channel as read like this on the client-side:

val channelClient = chatClient.channel(cid = "<channel_cid>")
channelClient.markRead().enqueue { result ->
    if (result is Result.Success) {
        // Channel marked as read
    } else {
        // Handle Result.Failure
    }
}

Or from server-side by providing a user id:

await channel.markRead({ user_id: "foo" });

Last read message

The last_read_message_id field tells which was the last read message that a given user read in the channel.

Read state

When you retrieve a channel from the API (e.g. using query channels), the read state for members is included in the response (up to 100 members, ordered by the most recent added, the current user’s read state is always included).

The read state includes the number of unread messages, the last read message id, and when the user read the channel. An example:

val channelClient = chatClient.channel(cid = "<channel_cid>")
channelClient.get(
    messageLimit = 10,
    memberLimit = 10,
    state = true
).enqueue { result ->
    if (result is Result.Success) {
        val channel = result.value
        val channelRead = channel.read.find { it.user.id == "<user_id>" }
        if (channelRead != null) {
            // When the user read the channel
            println(channelRead.lastRead)
            // ID of the last read message
            println(channelRead.lastReadMessageId)
            // Number of unread messages for the user in the channel
            println(channelRead.unreadMessages)
        }
    } else {
        // Handle Result.Failure
    }
}

The last read message id is updated in the following events:

  • message.read

  • notification.mark_read

  • notification.mark_unread

val channelClient = chatClient.channel(cid = "<channel_cid>")
channelClient.watch().enqueue()

// Any member read the channel
channelClient.subscribeFor<MessageReadEvent> { event ->
    println(event.lastReadMessageId)
    // Unread messages: 0
}

// Connected user read the channel
channelClient.subscribeFor<NotificationMarkReadEvent> { event ->
    println(event.lastReadMessageId)
    // Unread messages: 0
}

// Connected user marked a message as unread
channelClient.subscribeFor<NotificationMarkUnreadEvent> { event ->
    println(event.lastReadMessageId)
    // Unread messages
    println(event.unreadMessages)
}

Jump to last read message

This is how you can jump to the last read message inside a given channel:

const channel = client.channel("messaging", "test");
await channel.watch();

const lastReadMessageId = channel.state.read["<user id>"];
await channel.state.loadMessageIntoState(lastReadMessageId);

console.log(channel.state.messages);

Control read state exposure

Please take in account that user’s read state exposure can be controlled by user privacy settings:

// user object with privacy settings where read receipts are disabled
{
  // other user fields
  "privacy_settings": {
    "read_receipts": {
      "enabled": false
    }
  }
}

If privacy_settings.read_receipts.enabled is set to false, then the read state of this user will not be exposed to others. Additionally, read state related events, such as message.read and notification.mark_read, will not be delivered to others when this user reads messages.

© Getstream.io, Inc. All Rights Reserved.