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

Bug: Cannot get unread count per channel #1104

Closed
MaxAst opened this issue Mar 10, 2023 · 7 comments
Closed

Bug: Cannot get unread count per channel #1104

MaxAst opened this issue Mar 10, 2023 · 7 comments

Comments

@MaxAst
Copy link

MaxAst commented Mar 10, 2023

I have a list where each list item component has the following useEffect:

useEffect(() => {
    const channel = chatClient.channel("messaging", id);
    setUnread(channel.countUnread())
    const listener = channel.on((event) => {
      if (event.type === "notification.message_new" && event.unread_count) {
        setUnreads(event.unread_count);
      }
    });
    return () => {
      listener.unsubscribe();
    };
}, [id]);

When another channel member sends a message into the given channel, channel.countUnread() returns 0 and the event listener contains the user's total unreads on both fields, total_unread_count and unread_count. I would have expected the latter to be the unread count for only the given channel. So I guess two things are broken:

  1. channel.countUnread() always returns 0 when the list item component mounts, even when the total_unread_count and unread_count are greater than 0 (I also verified that it can't be 0 by logging this at the beginning of the useEffect body: console.log(JSON.stringify(chatClient.user, null, 2));, which returned total_unread_count and unread_count greater than 0)
  2. total_unread_count and unread_count both return the unread count across all channels instead of unread_count only returning the unread of the given channel
@MaxAst
Copy link
Author

MaxAst commented Mar 10, 2023

FWIW, I managed to get the unread count per channel by doing this:

useEffect(() => {
    const channel = chatClient.channel("messaging", data.channelId);
    channel
      .watch()
      .then((state) => {
        const me = state.read?.find((r) => r.user.id === chatClient.user?.id);
        if (typeof me?.unread_messages === "number") {
          setUnreads(me?.unread_messages);
        }
      })
    const listener = channel.on((event) => {
      if (event.type === "notification.message_new" && event.unread_count) {
        setUnreads(event.unread_count);
      }
    });
    return () => {
      listener.unsubscribe();
    };
  }, [data.channelId]);

it works, but feels like a workaround rather than a feature. I'd prefer not having to watch the channel

@sterlingwes
Copy link

I logged a similar issue recently if maintainers see this #1103

(Thank you for sharing your workaround)

@sterlingwes
Copy link

@MaxAst do you mind sharing your stream-chat version number? I've tried your code above and const me = state.read is always undefined for me, not sure how you're getting the initial count

@MaxAst
Copy link
Author

MaxAst commented Mar 17, 2023

"stream-chat": "^8.4.1" with "stream-chat-expo": "^5.11.2"

I'm a bit worried as to why there hasn't been a reaction by the maintainers yet.. considering it's a core feature of a paid product 🤷

@santhoshvai
Copy link
Member

@MaxAst We found a bug in the RN SDK relevant to the unread count state. Could you please upgrade your Expo SDK to v5.12.0 and check if it has fixed the issue

@MaxAst
Copy link
Author

MaxAst commented Mar 27, 2023

@santhoshvai channel.countRead() works for me now, but now streamClient.user.total_unread_count, streamClient.user.total_unread_count, and streamClient.user.unread_channels are all 0, while channel.countRead() returns the correct value

@vanGalilea
Copy link
Contributor

Closing this in favour of #1119

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

4 participants