Join channels
Requires App Context
To set up and manage channel membership, you must enable App Context for your app's keyset in the Admin Portal.
join()
connects a user to a given channel and sets membership - this way, the chat user can both watch the channel's content and be its full-fledged member.
Method signature
join()
returns an asynchronous stream which produces a new event whenever the current user receives a new message in the channel they have just joined. The method subscribes the user to the channel and establishes a user-channel membership by creating a new MembershipImpl
object. The asynchronous stream serves as a message event listener, enabling real-time processing of incoming messages.
This method takes the following parameters:
channel.join(
custom: [String: JSONCodableScalar]? = nil
) -> (membership: MembershipImpl, messagesStream: AsyncStream<MessageImpl>)
Input
Parameter | Description |
---|---|
custom Type: [String: JSONCodableScalar] 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
Component | Type | Description |
---|---|---|
membership | MembershipImpl | Returned object containing info on the newly created user-channel membership. |
messagesStream | AsyncStream<MessageImpl> | An asynchronous stream that produces a new value every time a new message is published on the current channel. |
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.
Join the support
channel and mark this membership as premium
to add information about your support plan.
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
let joinResult = try await channel.join(custom: ["support_plan": "premium"])
debugPrint("Channel membership: \(joinResult.membership)")
debugPrint("Membership channel ID: \(joinResult.membership.channel.id)")
} else {
debugPrint("Channel not found")
}
}
Other usage
Join the support
channel and listen to new messages.
- AsyncStream
- Closure
// Assuming you have a reference of type "ChatImpl" named "chat"
Task {
if let channel = try await chat.getChannel(channelId: "support") {
// Join the channel
let joinResult = try await channel.join(custom: ["support_plan": "premium"])
// Continuously fetch and process values from the stream
for await message in joinResult.messageStream {
debugPrint("Received a new message: \(message)")
}
} else {
debugPrint("Channel not found")
}
}
// Important: Keep a strong reference to the returned "AutoCloseable" object as long as you want
// to receive new messages. If the "AutoCloseable" is deallocated, the stream will be cancelled,
// and no further items will be produced. You can also stop receiving messages manually
// by calling the "close()" method on the "AutoCloseable" object.
// Assuming you have a reference of type "ChannelImpl" named "channel"
closeable = channel.join(custom: ["support_plan": "premium"]) { message in
debugPrint("Received a new message: \(message)")
}