Channel Message Count

Message counting

Stream Chat can keep track of the number of messages in a channel. This is useful for displaying message counts in your UI.

Message counting is disabled by default and can be enabled on a channel type level by setting the count_messages option to true when creating or updating a channel type.

Message counting only works on newly created channels. Existing channels will not have their message count updated when this feature is enabled.

Enabling message counting

newChannelType := &ChannelType{
// Copy the default settings.
ChannelConfig: DefaultChannelConfig,
}
newChannelType.Name = "public"
newChannelType.CountMessages = true
client.CreateChannelType(newChannelType)

Retrieving message count

When message counting is enabled for a channel type, the message count can be retrieved from the channel object.

When you query or watch a channel, the message count will be available in the message_count field of the channel object.

When the `count_messages` setting is enabled for a channel and messages are added or deleted, every event generated as a result of those operations (e.g: `message.new` or `message.deleted`) will include the updated count in the `channel_message_count` field.
channel, _ := client.QueryChannel(ctx, "messaging", "general", nil)
fmt.Println(channel.MessageCount)
© Getstream.io, Inc. All Rights Reserved.