Ajantha Bandara
Ajantha Bandara

Reputation: 1531

how to initiate one-to-one chat with getstream.io?

I'm trying to create slack like application using getstream.io chat SDK. In the documentation we can find out, how to initiate a channel to start group chat. But there is no any information about one-to-one chat. Does anyone knows how to initiate a one-to-one chat?

code sample to create new channel for group chat

const client = new StreamChat('t5v25xyujjjq');
await client.setUser(
    {
        id: 'jlahey',
        name: 'Jim Lahey',
        image: 'https://i.imgur.com/fR9Jz14.png',
    },
    'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiamxhaGV5In0.sFe9WvuHIKgQmgtEWFh708Nu_a2wy8Zj3Q8BJz7fDRY',
);

const channel = chatClient.channel('messaging', 'groupname', {
    name: 'Founder Chat',
    image: 'profile image url',
    members: ['user1', 'user2'],
});
const response = await channel.sendMessage({
    text: 'Josh I told them I was pesca-pescatarian'
});

Upvotes: 1

Views: 1739

Answers (2)

Tommaso Barbugli
Tommaso Barbugli

Reputation: 12031

If you want to have the guarantee that only one channel exists between N users you can instantiate the Channel without ID and the list of its members.

When you do that, the API will ensure that only one channel with the list of members exists (order of the members does not matter).

const  distinctChannel = conversation.channel("messaging","", {
   members: [user1.id, user2.id],
});

await distinctChannel.create();

Since the channel data is going to be the same for both members; I suggest to not store the image field the way you do in your code example. It is easier to use the "other" member profile image as the channel image when you render the conversation.

For example:

let channelImage = "https://path/to/fallback/image";

otherMembers = channel.members.filter(member => member.user.id != currentUser.id);

if otherMembers.length > 0 {
   channelImage = otherMembers[0].image;
}

Upvotes: 1

Jayr
Jayr

Reputation: 608

Check the docs securely, it's in there:

  • check under channel initialization

    const conversation = authClient.channel('messaging', 'thierry-tommaso-1', { name: 'Founder Chat', image: 'bit.ly/2O35mws', members: ['thierry', 'tommaso'], });

Upvotes: 0

Related Questions