Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial unread message count is always zero #2635

Closed
sterlingwes opened this issue Mar 9, 2023 · 13 comments
Closed

initial unread message count is always zero #2635

sterlingwes opened this issue Mar 9, 2023 · 13 comments

Comments

@sterlingwes
Copy link

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
Copy link
Author

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

@sterlingwes
Copy link
Author

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
Copy link
Author

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

@santhoshvai
Copy link
Member

santhoshvai commented Mar 21, 2023

@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

@sterlingwes
Copy link
Author

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

@sterlingwes
Copy link
Author

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

@sterlingwes
Copy link
Author

still an issue with your update...

@santhoshvai
Copy link
Member

@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.

@sterlingwes
Copy link
Author

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

@vanGalilea
Copy link
Contributor

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

@MartinCupela MartinCupela transferred this issue from GetStream/stream-chat-js Aug 27, 2024
@isekovanic
Copy link
Contributor

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 !

@khushal87
Copy link
Member

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.

@bense30
Copy link

bense30 commented Jan 7, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants