Updating Channel Members

Adding & Removing Channel Members

Using the addMembers() method adds the given users as members, while removeMembers() removes them.

import Stream Chat

let controller = chatClient.channelController(for: .init(type: .messaging, id: "general"))

controller.addMembers(userIds: ["thierry", "josh"])
controller.removeMembers(userIds: ["tommaso"])

Note: You can only add/remove up to 100 members at once.

Members can also be added while creating the channel.

/// 1: Use the `ChatClient` to create a `ChatChannelController` with a list of user ids
let channelId = ChannelId(type: .messaging, id: "general")
let channelController = try chatClient.channelController(
  createChannelWithId: channelId,
  members: ["thierry", "tommaso"]
)
/// 2: Call `ChatChannelController.synchronize` to create the channel.
channelController.synchronize { error in
  if let error = error {
    /// 4: Handle possible errors
    print(error)
  }
}

Stream Chat has a soft cap of 3000 channel memberships per user. If your use case requires >3000 channel memberships per user, consider removing users from channels or using elevated permissions to allow a user to access channels without membership if your use case allows

Message parameter

You can optionally include a message object that client-side SDKs will use to populate a system message. This works for both add and remove members

channelController.addMembers(
  userIds: ["tommaso"],
  message: "Tommaso joined the channel"
) { error in
  // …
}

Hide history

When members join a channel you can specify if they have access to the history or not. The history will be shown by default, set true to hide_history parameter to hide it for new members.

channelController.addMembers(
  userIds: ["thierry"],
  hideHistory: true
) { error in
  // …
}

Leaving a channel

It is possible for user to leave the channel without moderator-level permissions. Make sure channel members have Leave Own Channel permission.

channelController.removeMembers(
  userIds: ["john"]
) { error in
  // …
}

You can familiarize yourself with all permissions in Permissions section

Adding & Removing Moderators to a Channel

Using the addModerators() method adds the given users as moderators (or updates their role to moderator if already members), while demoteModerators() removes the moderator status.

await channel.addModerators(["thierry", "josh"]);
await channel.demoteModerators(["tommaso"]);

These operations can only be performed server-side and up to 100 moderators can be added or removed at once.

© Getstream.io, Inc. All Rights Reserved.