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() accepts a callback function and a set of parameters as arguments. The Chat SDK invokes this callback whenever the current user receives a new message on a channel they have just joined. It subscribes the user to a channel and adds a message event listener underneath. The method also sets user and channel membership by creating a new Membership object that holds the user-channel relationship.

This method takes the following parameters:

channel.join(
custom: [String: JSONCodableScalar]? = nil,
callback: ((MessageImpl) -> Void)? = nil,
completion: ((Swift.Result<(membership: MembershipImpl, disconnect: AutoCloseable?), Error>) -> Void)? = nil
)

Input

ParameterTypeRequiredDefaultDescription
custom[String: JSONCodableScalar]Non/aAny 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.
callback((MessageImpl) -> Void)Yesn/aCallback function passed as a parameter. It defines the custom behavior to be executed when detecting a new MessageImpl object on the joined channel.

Output

The returned Result object contains the membership and disconnect fields:

ParameterTypeDescription
membershipMembershipImplReturned object containing info on the newly created user-channel membership.
disconnectAutoCloseableFunction that lets you stop listening to new channel messages or message updates while remaining a channel membership. This might be useful when you want to stop receiving notifications about new messages or limit incoming messages or updates to reduce network traffic.

Basic usage

Join the support channel and mark this membership as premium to add information about your support plan.

/// Assuming you have a "chat" instance available
/// Fetch metadata of the "support" channel
chat?.getChannel(
channelId: "support"
) {
switch $0 {
case let .success(channel):
if let channel = channel {
debugPrint("Fetched channel metadata with ID: \(channel.id)")
/// Join the channel and mark the membership as "premium"
channel.join(
custom: ["support_plan": "premium"],
callback: nil
) {
switch $0 {
show all 28 lines
Last updated on