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.

Method signature

This method takes the following parameters:

channel.invite(
user: UserImpl,
completion: ((Swift.Result<MembershipImpl, Error>) -> Void)? = nil
)

Input

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

Output

TypeDescription
MembershipImplReturned (modified) object containing the membership data.

Basic usage

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

/// Assuming you have a `chat` instance available
/// Get the user instance for "support-agent-15"
chat?.getUser(userId: "support-agent-15") { [weak chat] in
switch $0 {
case let .success(user):
if let user = user {
/// User found, proceed to get the channel instance
debugPrint("Fetched user with ID: \(user.id)")
/// Get the channel instance for "high-prio-incidents"
chat?.getChannel(channelId: "high-prio-incidents") {
switch $0 {
case let .success(channel):
if let channel = channel {
/// Channel found, proceed to invite the user
debugPrint("Fetched channel with ID: \(channel.id)")
show all 38 lines

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: [UserImpl],
completion: ((Swift.Result<[MembershipImpl], Error>) -> Void)? = nil
)

Input

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

Output

TypeDescription
[MembershipImpl]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.

/// Assuming you have a "chat" instance available

/// Get the user instance for "support-agent-15"
chat?.getUser(userId: "support-agent-15") {
switch $0 {
case let .success(user15):
if let user15 = user15 {
/// User support-agent-15 found, proceed to get the user instance for "support-agent-16"
debugPrint("Fetched user with ID: \(user15.id)")
chat?.getUser(userId: "support-agent-16") { [weak chat] in
switch $0 {
case let .success(user16):
if let user16 = user16 {
/// Both users found, proceed to get the channel instance
debugPrint("Fetched user with ID: \(user16.id)")
show all 53 lines

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 or to any selected channel.

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<T: EventContent>(
type: T.Type,
channelId: String,
customMethod: EmitEventMethod = .publish,
callback: @escaping ((EventWrapper<T>) -> Void)
) -> AutoCloseable
Input
ParameterTypeRequiredDefaultDescription
typeT.TypeYesn/aType parameter allowing access to type information at runtime.
channelIdStringYesn/aChannel to listen for new Invite events.
customMethodEmitEventMethodNon/aAn optional custom method for emitting events. If not provided, defaults to .publish. Available values: .publish and .signal.
callback@escaping ((EventWrapper<T>) -> Void)Yesn/aA lambda function that is called with an EventWrapper<T> as its parameter. It defines the custom behavior to be executed whenever an event is detected on the specified channel.
Output
TypeDescription
@escaping ((EventWrapper<T>) -> Void))Sets up a listener for events of a specified type on a given channel and invokes a callback upon receiving an event.

Basic usage

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

// Assuming you have a "chat" instance available
// Invoke the "listenForEvents()" method to listen for invitation events on the "userId" channel
let listener = chat.listenForEvents(
type: EventContent.Invite.self,
channelId: "userId",
customMethod: .signal
) {
print(
"Received an invitation on channel \($0.event.payload.channelId) of type \($0.event.payload.channelType)"
)
}
Last updated on