Invite users to channels

Requires App Context

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

Let users invite one or more people to a private or group conversation and sets their channel membership.

As a result or inviting one or more users to a direct or group channel, an event of the invite type gets created. You can listen to these events in your chat app and notify the invited users.

Not available for public chats

Invitations are disabled in public chats. If you try implementing this feature in a public channel type, you'll get the Channel invites are not supported in Public chats error.

Chat SDK doesn't provide any default logic that is triggered when one user invites another user to join a channel. However, if you want the invited user to receive a notification about the new invitation, you can implement custom logic in your chat app that will:

Invite one user

invite() requests another user to join a channel and become its member.

icon

Under the hood

Method signature

This method takes the following parameters:

channel.invite(user: User): Promise<Membership>

Input

ParameterTypeRequiredDefaultDescription
userUserYesn/aUser that you want to invite to a 1:1 channel.

Output

TypeDescription
Promise<Membership>Returned (modified) object containing the membership data.

Basic usage

Invite support-agent-15 to join the high-prio-incidents channel.

// reference "support-agent-15"
const user = await chat.getUser("support_agent_15")

// reference the "high-prio-incidents" channel
const channel = await chat.getChannel("high-prio-incidents")

// invite the agent to join the channel
await channel.invite(user)

Invite multiple users

inviteMultiple() requests other users to join a channel and become its members. You can invite up to 100 users at once.

icon

Under the hood

Method signature

This method takes the following parameters:

channel.inviteMultiple(users: User[]): Promise<Membership[]>

Input

ParameterTypeRequiredDefaultDescription
usersUser[]Yesn/aList of users you want to invite to the group channel. You can invite up to 100 users in one call.

Output

TypeDescription
Promise<Membership[]>Returned (modified) list of objects containing the membership data.

Basic usage

Invite support-agent-15 and support-agent-16 to join the high-prio-incidents channel.

// reference both agents
const user1 = await chat.getUser("support_agent_15")
const user2 = await chat.getUser("support_agent_16")

// reference the "high-prio-incidents" channel
const channel = await chat.getChannel("high-prio-incidents")

// invite both agents to join the channel
await channel.inviteMultiple([
user1,
user2
])

Listen to invite events

As an admin of your chat app, you can use the listenForEvents() method to monitor all events emitted when someone invites a person to a direct or group channel. You can use this method to send notifications to the invited users.

Events documentation

To read more about the events of type invite, refer to the Chat events documentation.

Method signature

This method has the following parameters:

chat.listenForEvents({
channel: string;
type?: "invite";
callback: (event: Event<"invite">) => unknown;
}): () => void
Input
ParameterTypeRequiredDefaultDescription
channelstringYesn/aChannel to listen for new invite events.
typestringNon/aType of events. invite is the type defined for all events emitted when an offensive message is flagged/reported.
callbackn/aYesn/aCallback function passed as a parameter. It defines the custom behavior to be executed whenever an invite event type is detected on the specified channel.
Output
TypeDescription
() => voidFunction you can call to disconnect (unsubscribe) from the channel and stop receiving invite events.

Basic usage

Print a notification for an invitation received on the support channel.

const chat = {
listenForEvents: async (config) => {
const simulateEvent = () => {
const event = "invite";
const eventData = {
channel: "support",
user: "Bob",
message: "Join our chat room!",
};
if (event === config.type) {
config.callback(eventData);
}
};

// simulate a single event when listening starts
show all 28 lines
Last updated on