Join channels

Requires App Context

To set up and manage channel membership, you must enable App Context for your app's keyset in the Admin Portal.

join() connects a user to a given channel and sets membership - this way, the chat user can both watch the channel's content and be its full-fledged member.

Interactive demo

Check how a sample implementation could look like in a React app showcasing user-channel membership.

Want to implement something similar?

Read how to do that or go straight to the demo's source code.

Test it out

Choose whether you want to join or leave a given channel and wait until you get notified when that happens.

Method signature

icon

Under the hood


join() accepts a callback function and a set of parameters as arguments. The Chat SDK invokes this callback whenever the current user receives a new message on a channel they have just joined. It subscribes the user to a channel and adds a message event listener underneath. The method also sets user and channel membership by creating a new Membership object that holds the user-channel relation.

This method takes the following parameters:

channel.join(
callback: (message: Message) => void,
{
custom?: ObjectCustom
}
): Promise<{
membership: Membership;
disconnect: () => void;
}>

Input

ParameterTypeRequiredDefaultDescription
callbackn/aYesn/aCallback function passed as a parameter. It defines the custom behavior to be executed when detecting a new Message object on the joined channel.
 → messageMessageYesn/aAny Message object received on the joined channel.
 → customObjectCustomNon/aAny custom properties or metadata associated with the channel-user membership in the form of a JSON. Values must be scalar only; arrays or objects are not supported. App Context filtering language doesn’t support filtering by custom properties.

Output

TypeDescription
Promise<Membership>Returned object containing all membership data.
ParameterTypeDescription
Promise<>objectReturned object containing two fields: membership and disconnect.
 → membershipMembershipReturned object containing info on the newly created user-channel membership.
 → disconnect() => voidFunction that lets you stop listening to new channel messages or message updates while remaining a channel membership. This might be useful when you want to stop receiving notifications about new messages or limit incoming messages or updates to reduce network traffic.

Basic usage

Join the support channel and mark this membership as premium to add information about your support plan.

// reference the "channel" object
const channel = await chat.getChannel("support")

// join the channel and add metadata to the newly created membership
await channel.join(
(data) => {
console.log(
"This is my first message on this channel! Nice to meet you all!", data
)
},
{
custom: {support_plan: "premium"}
}
)
Last updated on