-
Notifications
You must be signed in to change notification settings - Fork 78
Cheatsheet
- How to check if distinct channel exist between two or more (fixed number) of users
- Whats the difference between "client.connectUser" and "client.setUser"?
- Whats the difference between "new StreamChat('api_key')" and "StreamChat.getInstance('api_key')"?
- How to check if channel is being watched?
- How to increase timeout on API calls?
- How to query all the messages, where user is mentioned?
- How to get total unread count? And keep it updated within app?
- What are "connect events"? Toggle you see on dashboard
- How to query all the channels where I am invited and didn't accept yet
- How to run moderation for messages sent server-side
// 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.
}
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.
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.
const isActive = channel.initialized;
const client = StreamChat.getInstance('apiKey', { timeout: 10000 }); // default is 3000 ms
// 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
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);
}
});
Connect events are related to
- user watching a channel
user.watching.start
user.watching.stop
- changes to user's online status
user.presence.changed
await chatClient.queryChannels({
invite:pending
})
const { message } = await channel.sendMessage({
text,
attachments,
user_id: sentBy,
}, {
force_moderation: true
});
-- more things coming