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.
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: UserImpl
) async throws -> MembershipImpl
Input
Parameter | Description |
---|---|
user *Type: UserImpl Default: n/a | User that you want to invite to a 1:1 channel. |
Output
Parameter | Description |
---|---|
MembershipImpl | Returned (modified) object containing the membership data. |
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Invite support-agent-15
to join the high-prio-incidents
channel.
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
let supportAgent = try await chat.getUser(userId: "support-agent-15")
let channel = try await chat.getChannel(channelId: "high-prio-incidents")
if let supportAgent, let channel {
let channelMembership = try await channel.invite(user: supportAgent)
debugPrint("Updated membership: \(channelMembership)")
debugPrint("Membership channel id: \(channelMembership.channel.id)")
} else {
debugPrint("Either user or channel not found")
}
}
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]
) async throws -> [MembershipImpl]
Input
Parameter | Description |
---|---|
users *Type: [UserImpl] Default: n/a | List of users you want to invite to the group channel. You can invite up to 100 users in one call. |
Output
Parameter | Description |
---|---|
[MembershipImpl] | Returned (modified) list of objects containing the membership data. |
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Invite support-agent-15
and support-agent-16
to join the high-prio-incidents
channel.
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
let supportAgent15 = try await chat.getUser(userId: "support-agent-15")
let supportAgent16 = try await chat.getUser(userId: "support-agent-16")
let channel = try await chat.getChannel(channelId: "high-prio-incidents")
if let supportAgent15, let supportAgent16, let channel {
let channelMembership = try await channel.inviteMultiple(users: [supportAgent15, supportAgent16])
debugPrint("Updated memberships: \(channelMembership)")
} else {
debugPrint("Either channel, or users, not found")
}
}
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,
) -> AsyncStream<EventWrapper<T>>
Input
Parameter | Description |
---|---|
type *Type: T.Type Default: n/a | Type parameter allowing access to type information at runtime. |
channelId *Type: String Default: n/a | Channel to listen for new Invite events. |
customMethod Type: EmitEventMethod Default: n/a | An optional custom method for emitting events. If not provided, defaults to .publish . Available values: .publish and .signal . |
Output
Parameter | Description |
---|---|
AsyncStream<EventWrapper<T>> | An asynchronous stream that produces a value each time a new event of the specified type is detected. |
Basic usage
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
Print a notification for an invitation received on the userId
channel.
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
for await event in chat.listenForEvents(type: EventContent.Invite.self, channelId: "userId") {
debugPrint("Received an invitation on channel ID: \(event.event.payload.channelId)")
debugPrint("Channel type: \(event.event.payload.channelType)")
}
}