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:
- Create and send custom events when invitations are sent.
- Let invited users receive these custom events upon sending invitations.
Invite one user
invite()
requests another user to join a channel and become its member.
Method signature
This method takes the following parameters:
channel.invite(user: User): Promise<Membership>
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
user | User | Yes | n/a | User that you want to invite to a 1:1 channel. |
Output
Type | Description |
---|---|
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.
Method signature
This method takes the following parameters:
channel.inviteMultiple(users: User[]): Promise<Membership[]>
Input
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
users | User[] | Yes | n/a | List of users you want to invite to the group channel. You can invite up to 100 users in one call. |
Output
Type | Description |
---|---|
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
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
channel | string | Yes | n/a | Channel to listen for new invite events. |
type | string | No | n/a | Type of events. invite is the type defined for all events emitted when an offensive message is flagged/reported. |
callback | n/a | Yes | n/a | Callback 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
Type | Description |
---|---|
() => void | Function 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