Cheatsheet - GetStream/stream-chat-js GitHub Wiki

Appendix

How to check if distinct channel exist between two or more (fixed number) of users

// Check if the channel already exists.
const channels = await chatClient.queryChannels({
    distinct: true,
    members: ['vishal', 'neil'],
});
if (channels.length === 1) {
    // Channel already exist
} else {
    // Channel doesn't exist.
}

Whats the difference between "client.connectUser" and "client.setUser"?

client.setUser is a deprecated version of client.connectUser or setUser is precursor of connectUser. This change was introduced in 2.10.0 since connectUser better defines the job of this function.

client.setUser has been deprecated, so please use client.connectUser instead.


Whats the difference between "new StreamChat('api_key')" and "StreamChat.getInstance('api_key')"?

new StreamChat('api_key') always returns a new instance of chat client, while StreamChat.getInstance('api_key') gives you a singleton instance of the client. We HIGHLY RECOMMEND you to use singleton instance to avoid creating multiple instances websocket connections, which in turn increases your monthly active users, and that affects your billing amount. Especially with react hooks, its quite easy to fall into these issues

Please check our best practices guide for details.


How to check if channel is being watched?

const isActive = channel.initialized;

How to increase timeout on API calls?

const client = StreamChat.getInstance('apiKey', { timeout: 10000 }); // default is 3000 ms

How to query all the messages, where user is mentioned?

// Search in all channels
const res = await chatClient?.search(
  {
      type: 'messaging'
  },
  {
    'mentioned_users.id': {
      $contains: 'vishal' || '',
  },
  {
      limit: 20,
      offset: 0,
  },
);

console.log(res.results); // array of messages

// Search in all channels, where user is member
const res = await chatClient?.search(
  {
      members: {
        $in: ['vishal' || null],
      },
  },
  {
    'mentioned_users.id': {
      $contains: 'vishal' || '',
  },
  {
      limit: 20,
      offset: 0,
  },
);

console.log(res.results); // array of messages

How to get total unread count? And keep it updated within app?

To get number of unread channels, on load:

const user = await client.connectUser({ id: 'myid' }, token);
// response.me.total_unread_count is the total unread count
// response.me.unread_channels is the count of channels with unread messages

To keep track of count:

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

What are "connect events"? Toggle you see on dashboard

Connect events are related to

  • user watching a channel
    • user.watching.start
    • user.watching.stop
  • changes to user's online status
    • user.presence.changed

How to query all the channels where I am invited and didn't accept yet

await chatClient.queryChannels({
  invite:pending
})
-- more things coming