Changing Channel Members

LAST EDIT Apr 08 2021

Adding & Removing Channel Members

Copied!

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

1
2
await channel.addMembers(['thierry', 'josh']); 
await channel.removeMembers(['tommaso']);
1
2
await channel.addMembers(["thierry", "josh"]); 
await channel.removeMembers(["tommaso"]);
1
2
$channel->addMembers(['thierry', 'jenny']); 
$channel->removeMembers(['thierry', 'jenny']);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
val channelClient = client.channel("messaging", "general") 
 
// Add members with ids "thierry" and "josh" 
channelClient.addMembers("thierry", "josh").enqueue { result -> 
    if (result.isSuccess) { 
        val channel: Channel = result.data() 
    } else { 
        // Handle result.error() 
    } 
} 
 
// Remove member with id "tommaso" 
channelClient.removeMembers("tommaso").enqueue { result -> 
    if (result.isSuccess) { 
        val channel: Channel = result.data() 
    } else { 
        // Handle result.error() 
    } 
}
1
2
3
4
5
6
import Stream Chat 
 
let controller = chatClient.channelController(for: .init(type: .messaging, id: "general")) 
 
controller.addMembers(userIds: ["thierry", "josh"]) 
controller.removeMembers(userIds: ["tommaso"])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ChannelClient channelClient = client.channel("messaging", "general"); 
 
// Add members with ids "thierry" and "josh" 
channelClient.addMembers("thierry", "josh").enqueue(result -> { 
    if (result.isSuccess()) { 
        Channel channel = result.data(); 
    } else { 
        // Handle result.error() 
    } 
}); 
 
// Remove member with id "tommaso" 
channelClient.removeMembers("tommaso").enqueue(result -> { 
    if (result.isSuccess()) { 
        Channel channel = result.data(); 
    } else { 
        // Handle result.error() 
    } 
});
Note: You can only add/remove up to 100 members at once.

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

1
2
3
4
// using client-side client 
await channel.addMembers(['tommaso'], { text: 'Tommaso joined the channel.' });  
// using server-side client 
await channel.addMembers(['tommaso'], { text: 'Tommaso joined the channel.', user_id: 'tommaso' });

Leaving a channel

Copied!

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

1
2
// remove own channel membership 
await channel.removeMembers(['my_user_id']);
You can familiarize yourself with all permissions in Permissions section

Adding & Removing Moderators to a Channel

Copied!

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.

1
2
await channel.addModerators(['thierry', 'josh']); 
await channel.demoteModerators(['tommaso']);
1
2
channel.add_moderators(["thierry", "josh"]); 
channel.demote_moderators(["tommaso"]);
1
2
channel.add_moderators(["thierry", "josh"]); 
channel.demote_moderators(["tommaso"]);
1
2
$channel->addModerators(['thierry', 'jenny']); 
$channel->demoteModerators(['thierry', 'jenny']);
1
// at the moment we don't have a Java client for server side usage
1
2
await channel.AddModerators(new string[] { "thierry", "josh" }); 
await channel.DemoteModerators(new string[] { "tommaso" });
1
2
3
4
newModerators := []string{"bob", "sue"} 
err = channel.AddModerators("thierry", "josh") 
err = channel.AddModerators(newModerators...) 
err = channel.DemoteModerators(newModerators...)
These operations can only be performed server-side and up to 100 moderators can be added or removed at once.