1-1 Chat

Enable in-app private conversations and see connections start from your platform

Having a direct chat between two people is very easy with the Amity Chat SDK.

The key is to ensure that a standard channelId is set between two pairs of users.

The channelId is unique and immutable to the channel. We need to make sure the same pair of users always produces the same channelId. To to this, we will write a simple function that takes in two userId parameters and returns a string that will be the channelId. The logic for generating the string should be commutative, meaning that the pair of userId parameters can be given in any order, it will still return the same hash.

function generateChannelId(userA, userB) {
return 'directchat-' + min(userA, userB) + '-' + max(userA, userB);
}
const channelId = generateChannelId('alice', 'bob');
// channelId will be 'directchat-alice-bob', regardless of if 'alice' is first parameter or 'bob' is first parameter

This channelId can then be passed to a join() or create() method to fetch the final direct chat channel.