2

Is there any way to query a users total unread messages count across all channels from the server?

I can see it is possible to do this from the client using setUser but this is not appropriate for usage in a server side scenario. I am using nodejs, any suggestions would be appreciated.

Thanks, Mark

TreeMan360
  • 589
  • 1
  • 5
  • 16

1 Answers1

4

This is how I got all the messages across all channels from the server. I wrote it in JS, hopefully it will help you out:


async function listUnreadMessages(user) {
  const serverClient = new StreamChat(api_key, stream_secret, options)
  await serverClient.setUser(
    {
      id: `${user.id}`,
      name: `${user.full_name}`,
      image: user.profile_image
    },
    user.chat_token
  )

  const filter = { members: { $in: [`${user.id}`] } }
  const sort = { last_message_at: -1 }
  const channels = await serverClient.queryChannels(filter, sort, {
    watch: true
  })
  let unreadList = {}
  const unreads = await Promise.all(
    channels.map((c) => {
      unreadList[c.id] = c.countUnread()
      return c.countUnread()
    })
  )
  serverClient.disconnect()
  return unreadList
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Kryptman
  • 996
  • 9
  • 14
  • As of March 2022 this feature doesn't seem available on the server-side. The answer throws errors such as: `order channels by unread_count or has_unread is not support server-side`. – ssbarbee Mar 08 '22 at 20:44