Updating Channel Members

Adding & Removing Channel Members

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

// Android SDK

ChannelClient channelClient = client.channel("messaging", "general");

// Add members with ids "thierry" and "josh"
channelClient.addMembers(Arrays.asList("thierry", "josh"), null).enqueue(result -> {
  if (result.isSuccess()) {
    Channel channel = result.data();
  } else {
    // Handle result.error()
  }
});

// Remove member with id "tommaso"
channelClient.removeMembers(Arrays.asList("tommaso"), null).enqueue(result -> {
  if (result.isSuccess()) {
    Channel channel = result.data();
  } else {
    // Handle result.error()
  }
 });

// Backend SDK

// Add members with ids "thierry" and "josh"
Channel.update("messaging", "general")
  .addMembers(Arrays.asList("thierry", "josh"))
  .request();
});

// Remove member with id "tommaso"
Channel.update("messaging", "general")
  .removeMember("tommaso")
  .request();
});

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

Members can also be added while creating the channel.

const channel = client.channel('messaging', randomID, {
  members: [
    { user_id: "james_bond", code_name: "007" },
    { user_id: "alec_trevelyan", code_name: "006" },
    { user_id: "bill_fairbanks", code_name: "002" },
});
await channel.create();

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

// Android SDK
ChannelClient channelClient = client.channel("messaging", "general");

Message addMemberSystemMessage = new Message();
addMemberSystemMessage.setText("Thierry and Josh were added to this channel");
// Add members with ids "thierry" and "josh"
channelClient.addMembers(Arrays.asList("thierry", "josh"), addMemberSystemMessage).enqueue(result -> {
  if (result.isSuccess()) {
    Channel channel = result.data();
  } else {
    // Handle result.error()
  }
});

Message removeMemberSystemMessage = new Message();
addMemberSystemMessage.setText("Tommaso was removed from this channel");
// Remove member with id "tommaso"
channelClient.removeMembers(Arrays.asList("tommaso"), removeMemberSystemMessage).enqueue(result -> {
  if (result.isSuccess()) {
    Channel channel = result.data();
  } else {
    // Handle result.error()
  }
 });

// Backend SDK
MessageRequestObject msg = MessageRequestObject
  .builder()
    .text("Thierry and Josh were added to this channel")
  .build();
// Add members with ids "thierry" and "josh"
ChannelUpdateResponse resp = Channel.update("messaging", "general")
  .addMembers(Arrays.asList("thierry", "josh"))
  .message(msg)
  .request();

MessageRequestObject msg = MessageRequestObject
  .builder()
    .text("Tommaso was removed from this channel")
  .build();
// Remove member with id "tommaso"
ChannelUpdateResponse resp = Channel.update("messaging", "general")
  .removeMember("tommaso")
  .message(msg)
  .request();

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.

// Backend SDK
Channel.update("channel-type", "channel-type")
  .addMember("thierry")
  .hideHistory(true)
  .request();

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.

// Backend SDK
Channel.update("channel-type", "channel-type")
  .removeMember("my_user_id")
  .request();

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.

// Backend SDK
Channel.update("channel-type", "channel-type").addModerator("thierry").addModerator("josh").request();
Channel.update("channel-type", "channel-type").demoteModerator("tommaso").request();

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.