GetStream / stream-chat-js

JS / Browser Client - Build Chat with GetStream.io
https://getstream.io/chat/
Other
182 stars 76 forks source link

countUnread strange behaviour #105

Closed aniciom closed 5 years ago

aniciom commented 5 years ago

Hello, i'm having an issue the situation is the next, i have a frontend chat using react and a backend server to handle the list of channels and their unread messages per client, the problem what i'm facing its the next:

1 - I go to my chat i click on the channel's box then i send using the client something like:

const selectedChannel = client.channel("messaging", channel.id);
selectedChannel.markRead();

2 - I get a list from my backend of my channels using this code on nodejs (i'm using the client server-side):

const channels = await streamChatClient.queryChannels(
      {},
      { last_message_at: -1, created_at: -1 },
      {}
    );

    channels.map(channel => {

      console.log(channel.state.read);

      chanListResponse.push({
        id: channel.data.id,
        cid: 'messaging:'+channel.data.id,
        name: channel.data.chanName,
        type: channel.data.chanType,
        created_by: channel.data.created_by.name,
        unread: channel.countUnread(),
      });
    });

3 - The first time i do markRead from the frontend and then i do a channel list from the backend and get all the channels data, i get 0 from countUnread and the channel.state.read seems to be filled with the right information.

Now the problem is once i restart my node server, this information seems to get lost and the state.read's object comes empty from the endpoint when i do the list channels's call, so i've to do a markRead again from the frontend and then the state.read's object gets filled with the right information, and the countUnread value is set to 0, any clue about this behaviour ? I also tried using markRead server side and the result is the same.

It seems each time the StreamChat's object gets instantiated it lost the reference to the last read's object, i don't know if its a normal or a flaw, but the thing is i can't automatically read all the messages for the user at startup time, the user should be able to know which ones were read and which ones weren't, and should be able to markRead some channels and some channels not, and i can't rely only in 1 instance because the server may get restarted and then i should mark read all them again. I hope the problem is clear, otherwise it pushes me to do some extra work in my side, grabbing all the channels and keep updating their latest messages into a local db with an unread counter.

Thanks in advance.

tbarbugli commented 5 years ago

Read state is only stored for channel members; non-members can still use mark_read to keep the read state (ie. a livestream chat).

Is the user added as a member in your case?

aniciom commented 5 years ago

Ah no its not

aniciom commented 5 years ago

It seems to be working as expected now, Thanks.