A simple flow of how to send a message -
Client-side
1. Instantiate the client
2. Call connectUser, pass the token received from the server-side
3. Create a channel/query channel
4. Send a message
const StreamChat = require(‘stream-chat’).StreamChat;
const appID = ‘app-id’;
const userID=‘username’;
const token=‘user-token obtained from the server-side’
const client = StreamChat.getInstance(appID);
const test = async () => {
const userDetails = await client.connectUser({
id: userID,
}, token);
const conversation = client.channel(‘messaging’, ‘Testingchannel’, {
members: [‘user1’,’user2’],
})
await conversation.create();
const filters = { type: ‘messaging’ , members: { $in: ['Seetha'] } };
const sort = { last_message_at: -1 };
const channels = await client.queryChannels(filters, sort);
const message1 = await conversation.sendMessage({
text: ‘Helloooo’ + userID,
mentioned_users: [‘user1’, ‘user2’],
anotherCustomField: 123
});
}
We have a server-side which instantiates the Stream client with a secret and all the tokens will be issued from here. We have a few specific actions performed from the server-side only like updating roles, updating channel type, etc.
Please refer to this article for more information on client-side / server-side + token generation -
https://getstream.zendesk.com/hc/en-us/articles/360054926754-Client-Side-and-Server-Side-and-Token-Creation
How do the Chat Client, Server, & Stream API communicate with each other?https://getstream.zendesk.com/hc/en-us/articles/360061669873-How-do-the-Chat-Client-Server-Stream-API-communicate-with-each-other?
Comments
0 comments
Please sign in to leave a comment.