stream-chat-react-native icon indicating copy to clipboard operation
stream-chat-react-native copied to clipboard

initial unread message count is always zero

Open sterlingwes opened this issue 2 years ago • 11 comments

I'm trying to do a simple thing:

  • in our app before a user sees the chat UI I want to mark how many unread messages there are for a specific channel (the user could be in multiple, I want separate counts for each)

Given the above, I figured I would call countUnread() on the channel to get the initial count of unread messages.

To test this I setup a channel with two users and had one user send a message to the channel before I loaded the UI in question for the second user.

Problem: I can not get the initial unread count to be anything but 0

I've tried the following code in our React Native app and in a minimal repro web app with the same result:

const jwt = process.env.NEXT_PUBLIC_GS_JWT
const userId = process.env.NEXT_PUBLIC_GS_UID

const testGetStream = async () => {
  const client = StreamChat.getInstance(API_KEY)
  await client.connectUser({id: userId ?? ''}, jwt)

  const channels = await client.queryChannels({
    type: 'messaging',
    members: {$in: [userId ?? '']},
  })
  console.log(
    channels.map((channel) => ({
      id: channel.id,
      readInfo: channel.state.read,
    })),
  )

  const channel = channels.find((channel) => channel.id === 'MY_CHANNEL_ID')
  if (channel) {
    channel.sendMessage({text: 'hiya'})
  }
}

testGetStream()

As you can see, when the page loads I log the channel read state (thought reaching into state might give me more visibility than the count unread method would) and send a message to the channel. Second page to load should see an unread count of 1 due to the first page load sendMessage.

In the log output I see zeros for anything related to unread message counts. The last_read timestamp seems to be the time the page loaded, oddly enough - so it seems something in your backend system or in this library is marking the channel read before I can get an unread count.

sterlingwes avatar Mar 09 '23 21:03 sterlingwes

I should add that this with stream-chat 8.4.1 and yes I also tried passing state: true to the queryChannels options

sterlingwes avatar Mar 09 '23 21:03 sterlingwes

Confirmed options being sent in the API request:

Screenshot 2023-03-09 at 4 25 34 PM

Smells like a client-side issue to me, since it seems you're organizing messages based on some timestamp watermark. What I did note is that the last_active date for the user I load the channels for is always updated to the load time even if in this case it's unclear the user actually read the messages when I could just be looking at a channel list.

I'm referencing the date I see under channels[].membership.user.last_active in the API response for the query

sterlingwes avatar Mar 09 '23 21:03 sterlingwes

@vanGalilea can you help me with this? i notice you looked into a related issue recently

sterlingwes avatar Mar 20 '23 15:03 sterlingwes

@sterlingwes We found a bug in the RN SDK relevant to the unread count state. Could you please upgrade your RN SDK to v5.12.0 and check if its fixed

santhoshvai avatar Mar 21 '23 15:03 santhoshvai

@santhoshvai as I've noted here this is reproducible on web, pls confirm this is the same issue

sterlingwes avatar Mar 21 '23 15:03 sterlingwes

I looked at the changelog you linked and that tells me you haven't even read this issue to understand my problem

sterlingwes avatar Mar 21 '23 15:03 sterlingwes

still an issue with your update...

sterlingwes avatar Mar 21 '23 16:03 sterlingwes

@sterlingwes Apologies here, I did fix a relevant issue in RN SDK related to stale unread counts and I read your issue only after that.

We will investigate this scenario and get back to you.

santhoshvai avatar Mar 22 '23 08:03 santhoshvai

hey @santhoshvai do you have an ETA on your investigation for this?

sterlingwes avatar Mar 29 '23 15:03 sterlingwes

@sterlingwes this is still in our backlog, please follow this issue for future updates.

vanGalilea avatar Apr 26 '23 17:04 vanGalilea

Hi @sterlingwes , I spent quite a bit of time debugging this and trying to reproduce the issue - but to no avail (went so far as to spin up both custom apps as well as using channel querying directly to see the values). In all instances, I have found that:

  • channel.countUnread() returns the correct value (for each user too, both in scenarios of programmatically triggering sendMessage as well as doing it manually within a channel)
  • channel.state.read[<some-user-id>] has all of the correct values (last_read seems consistent and correct, last_read_message_id is correct and unread_messages is correct and updating in real time for me)
  • I also tested the case of last_read being updated prematurely (while simply browsing the channels and not actually reading the messages) and this does not seem to be the case any longer

I'm under the impression that the issue most likely got fixed since it was reported (since it's really been a while, apologies for that !), or perhaps I'm doing something wrong while debugging. If you're available and up for it, would you be willing to give it a brief look on whether you can still reproduce this ?

If not then we can close the issue and reopen it if something similar still persists.

Thanks a ton for your analysis !

isekovanic avatar Aug 29 '24 10:08 isekovanic

Closing this issue for long time of inactivity and unabable to reproduce it. This might be fixed with past releases. Please upgrade to the latest version and let us know. Feel free to reopen it if you still see this as an issue.

khushal87 avatar Oct 14 '24 18:10 khushal87

I am having the same issue using stream-chat-expo. Just reinstalled to 5.44.2 and having the same issue.

After this code await channel.markUnread({ message_id: "<message_id>" }); the data in the event still shows total_unread_count, unread_count and unread_messages all as 0. unread_channels is set to 1.

bense30 avatar Jan 07 '25 21:01 bense30