On this page

Join channels

Requires App Context

Enable App Context on your keyset in the Admin Portal to manage memberships.

Use join() to connect a user to a channel and create a membership. The user can receive messages and is recorded as a channel member.

Interactive demo

Check what 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 relationship.

This method takes the following parameters:

1channel.join(
2 callback: (message: Message) => void,
3 {
4 custom?: ObjectCustom
5 }
6): Promise<{
7 membership: Membership;
8 disconnect: () => void;
9}>

Input

* required
ParameterDescription
callback *
Type: n/a
Default:
n/a
Callback function passed as a parameter. It defines the custom behavior to be executed when detecting a new Message object on the joined channel.
 → message *
Type: Message
Default:
n/a
Any Message object received on the joined channel.
 → custom
Type: ObjectCustom
Default:
n/a
Any 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.
ParameterDescription
Promise<>
Type: object
Returned object containing two fields: membership and disconnect.
 → membership
Type: Membership
Returned object containing info on the newly created user-channel membership.
 → disconnect
Type: () => void
Function 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.

Sample code

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

1// reference the "channel" object
2const channel = await chat.getChannel("support")
3
4// join the channel and add metadata to the newly created membership
5await channel.join(
6 (data) => {
7 console.log(
8 "This is my first message on this channel! Nice to meet you all!", data
9 )
10 },
11 {
12 custom: {support_plan: "premium"}
13 }
14)
Last updated on