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

Dev UX Bug · Knowing how many unread messages there are

Open charltongroves opened this issue 6 years ago • 10 comments

Describe the bug I have had to jump through an insane amount of hoops to get a simple 'unread notificaiton' badge count working in my app.

Here is all the code i've had to write.

(promise in this case is the promise that's returned from 'setUser' which i need to be resolved in order to figure out the initial 'total_unread_count')

type ChatBadgeProps = {|
  chatClient: ?{ client: ClientType, promise: Promise<mixed> },
|};

let globalUnreadTracker = null;
let globalUnreadListener = null;

function ChatBadge(props: ChatBadgeProps) {
  const [unreadChannels, setUnreadChannels] = React.useState(null);

  // This initialises the unread count with what our client returns on initialisation
  // If this component has been mounted previously, we will use the unread count kept in our
  // globalUnreadTracker. This is necessary as the client annoyingly doesnt keep a correct state of unread count,
  // so we have to do it ourselves.
  React.useEffect(() => {
    if (props.chatClient) {
      const refinedClient = props.chatClient;
      refinedClient.promise.then(() => {
        const newCount = refinedClient.client.user.total_unread_count || 0;
        const refined =
          globalUnreadTracker == null ? newCount : globalUnreadTracker;
        setUnreadChannels(refined);
        globalUnreadTracker = refined;
      });
    }
  }, [props.chatClient]);

  // This sets up a local and global listener to listen for changes to our unread count/
  // The local one exists so we can setState as changes come through
  // The global one exists so we can track changes that happened while this component is unmounted.
  // This means if we read a bunch of messages, we can record those unread_count changes, and then display
  // the correct value when we mount this component again.
  React.useEffect(() => {
    if (props.chatClient) {
      const callbackFunc = (event) => {
        if (event.total_unread_count !== undefined) {
          setUnreadChannels(event.total_unread_count);
          globalUnreadTracker = event.total_unread_count;
        }
      };
      const refinedClient = props.chatClient;
      refinedClient.client.on(callbackFunc);
      if (globalUnreadListener == null) {
        const globalCallbackFunc = (event) => {
          if (event.total_unread_count !== undefined) {
            globalUnreadTracker = event.total_unread_count;
          }
        };
        globalUnreadListener = globalCallbackFunc;
        refinedClient.client.on(globalUnreadListener);
      }
      return () => {
        refinedClient.client.off(callbackFunc);
      };
    }
    return () => {};
  }, [props.chatClient]);
  if (props.chatClient == null) {
    return null;
  }

  const total = unreadChannels || 0;
  if (total > 0) {
    return <Badge color={color.BUBBLE_URGENT} count={total} />;
  }
  return null;
}

A bit of an explanation as to whats going on: Currently there is no way to reliably query how many unread messages you have. You essentially have to wait until messages come through to see the latest 'count'. The problem is, when we unmount a component, that component cannot 'listen' for changes anymore, so when it gets remounted, it will have no idea how many unread messages there are (client.user.total_unread_count is only accurate immediately after setUser is called, and then is no longer reliable). The only way i could solve this is attaching a global listener to client that operates even when the badge component is unmounted. This 'attempts' to keep track of the unread count but can fail due to a few reasons. Eg, if you minimise the app, the app garbage collection can mean that the listener will stop firing. So when you reopen the app, you will either have to refire 'setUser' to get the latest count, or wait until an event fires to retrigger the listener.

Anyway, its very messy. Ideally the 'client' object would have a reliable up-to-date property that shows the actual unread count. That way you could just set up a timer that checks that value every 500ms and rerenders if it changes.

Dev environment info (please complete/provide the following information):

  • Package version: ^0.6.0
  • react-native or expo version
  • Device/Emulator (android/ios) and OS version

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior A clear and concise description of what you expected to happen.

Additional context Add any other context about the problem here.

Screenshots If applicable, add screenshots to help explain your problem.

charltongroves avatar Feb 24 '20 02:02 charltongroves

It's easy to get count unread messages. You could do something like this - https://getstream.io/chat/docs/unread/?language=js register the event during the componentDidUpdate and unregister (client.off) the event during componentWillUnmount

Also if you are using expo - you can also show count of unread message on your app icon -

client.on((event) => {
     if (event.total_unread_count !== undefined) {
         console.log(event.total_unread_count);
  Notifications.setBadgeNumberAsync(total_unread_count);
     }

     if (event.unread_channels !== undefined) {
         console.log(event.unread_channels);
     }
});

familywiser1 avatar Feb 25 '20 10:02 familywiser1

@familywiser1 I'm aware of that, it's not perfect though because if you re-mount, you'll need to wait until an event fires in order to get the latest unread message count, until that happens you will be showing whatever the unread message count was when you first fired 'setUser'.

I did exactly what you described in the code i attached, but i had to add a ton of workarounds to make it actually keep up to date, thats the entire point of this issue

charltongroves avatar Mar 10 '20 00:03 charltongroves

same issue here, is there another way to get the total account if not by setUser responseuserResponse.me.total_unread_count;?

mycode as example https://gist.github.com/rodrigowpl/c7b10c64ff949686321c37fcd1d5e02f#file-chat-js-L76

cc @vishalnarkhede

rodrigowpl avatar Dec 14 '20 19:12 rodrigowpl

@charltongroves @familywiser1 any update on your approach? facing the same issue here

rodrigowpl avatar Dec 15 '20 13:12 rodrigowpl

@rodrigowpl Whats your usecase here? Are you looking for separate api end point here to get unread count?

vishalnarkhede avatar Dec 15 '20 14:12 vishalnarkhede

Kinda @vishalnarkhede my issue is the same as @charltongroves reported:

This explains:

The problem is, when we unmount a component, that component cannot 'listen' for changes anymore, so when it gets remounted, it will have no idea how many unread messages there are (client.user.total_unread_count is only accurate immediately after setUser is called, and then is no longer reliable).

Currently, we just have the total unread when we call setUser that just happens once when we initialize the client. If you minimize the app and then open it again you will need to wait for trigger the unread event again to get the total information

rodrigowpl avatar Dec 15 '20 15:12 rodrigowpl

@vishalnarkhede another idea would be if you could retrieve the information when client connection is recovered

rodrigowpl avatar Dec 15 '20 16:12 rodrigowpl

@vishalnarkhede Any update here? This still happens

rodrigowpl avatar Jan 10 '21 12:01 rodrigowpl

hey guys, just an update: This solves the problem very well: https://github.com/GetStream/stream-chat-react-native/wiki/Cookbook-(2.x.x)#caveats

@charltongroves in the wait client._setupConnection() response you can get the unread counter updated

@vishalnarkhede I think we can close this issue

rodrigowpl avatar Jan 27 '21 11:01 rodrigowpl

@rodrigowpl thanks ! been looking for this :) but a little update, now the method is called openConnection(). in response we can get the response.me.total_unread_count

Bogdastotel avatar Nov 04 '23 23:11 Bogdastotel