Skip to content

Cheatsheet

Vishal Narkhede edited this page Feb 17, 2021 · 15 revisions

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

-- more things coming

Clone this wiki locally