Channel members

Add channel members

Members can be added to a channel either when creating it or by using the addMembers method.

When creating a channel

let channelController = try ChatClient.shared.channelController(
	createChannelWithId: randomId,
	members: [
		"userid1",
		"userid2",
		"userid3"
	]
)
// Calling synchronize() is required to persist the channel to the server.
channelController.synchronize()

Using addMembers method

// members can be added by passing an array of user IDs
let channelController = ChatClient.shared.channelController(for: randomId)
channelController.addMembers(userIds: ["userid1", "userid2", "userid3"])

// or by passing objects
let channelController = ChatClient.shared.channelController(for: randomId)
channelController.addMembers([
	MemberInfo(userId: "userid1"),
	MemberInfo(userId: "userid2"),
	MemberInfo(userId: "userid3", extraData: ["custom": .string("value")])
])

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

Message parameter

You can optionally include a message object to allow client-side SDKs to generate a system message. This feature is available for both adding and removing members.

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

Hide history

When members join a channel, you can specify whether they have access to the channel’s history.

By default, new members can see the history. To hide it, set the hide_history parameter to true.

channelController.addMembers(userIds: ['userid1'], hideHistory: true)

Channel member custom data

Custom data can be added at the channel member level. Ensure it does not exceed 5KB.

channelController.addMembers([
	MemberInfo(userId: "userid1", extraData: ["key1": .string("value1")]),
	MemberInfo(userId: "userid2", extraData: ["key2": .string("value2")]),
	MemberInfo(userId: "userid3", extraData: ["key3": .string("value3")])
])

Remove channel members / Leave a channel

Remove channel members

removeMembers method allows you to remove members from a channel.

channelController.removeMembers(userIds: ["userid1", "userid2"])

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

Leave a channel

Users can leave a channel without moderator-level permissions.

Ensure channel members have the Leave Own Channel permission enabled.

channelController.removeMembers(userIds: ["myuserid"])

You can familiarize yourself with all permissions in Permissions section

Add / Remove 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.

Add moderators

The addModerators method adds specified users as moderators to a channel. If the users are already members, their role is upgraded to moderator.

await channel.addModerators(["userid1", "userid2"]);

Remove moderators

The demoteModerators method removes the moderator role from specified users.

await channel.demoteModerators(["userid1"]);

These operations can only be performed server-side, and a maximum of 100 moderators can be added or removed at once.

Update channel members

Channel members can be partially updated. Only custom data and channel roles are eligible for modification.

You can set or unset fields, either separately or in the same call.

// set some fields
let memberController = ChatClient.shared.memberController(
	userId: someUserId,
	in: someChannelId
)
memberController.partialUpdate(extraData: [
	"key1": .string("value1"),
	"key2": .string("value2")
])

// unset some fields
memberController.partialUpdate(extraData: nil, unsetProperties: ["key1", "key2"])

// set / unset in the same call
memberController.partialUpdate(
	extraData: [
		"key1": .string("value1"),
		"key2": .string("value2"),
	],
	unsetProperties: ["key3"]
)

Query channel members

The queryMembers endpoint enables listing and paginating channel members. It offers filtering options to efficiently retrieve member information. This feature is particularly useful when you need to search through or display a comprehensive overview of channel membership.

Pagination and ordering

By default, members are ordered from oldest to newest and can be paginated using offset-based pagination or by the created_at or user_id fields.

While pagination by offset is the simplest to implement, it can lead to incorrect results if the list of members changes during pagination.

The recommended approach is to sort created_at or user_id for more reliable results.

// creates the controller and synchronizes the first batch of 25 members
let query = ChannelMemberListQuery(cid: channelId)
let memberListController = client.memberListController(query: query)
memberListController.synchronize()

// changing the default page size to 50
let query = ChannelMemberListQuery(cid: channelId, pageSize: 50)
let memberListController = client.memberListController(query: query)
memberListController.synchronize()

// changing the sort order by user_id in descending order
let query = ChannelMemberListQuery(cid: channelId, sort: [.init(key: .userId, isAscending: false)])
let memberListController = client.memberListController(query: query)
memberListController.synchronize()

Here’s some example of how you can query the list of members:

// query members by user.name
let query = ChannelMemberListQuery(cid: channelId, filter: .equal(.name, to: "tommaso"))
let memberListController = client.memberListController(query: query)
memberListController.synchronize()

// autocomplete members by user name
let query = ChannelMemberListQuery(cid: channelId, filter: .autocomplete(.name, text: "tomm"))
let memberListController = client.memberListController(query: query)
memberListController.synchronize()

// query member by id
let query = ChannelMemberListQuery(cid: channelId, filter: .equal(.id, to: "tommaso"))
let memberListController = client.memberListController(query: query)
memberListController.synchronize()

// query multiple members by id
let query = ChannelMemberListQuery(cid: channelId, filter: .in(.id, values: ["tommaso", "thierry"]))
let memberListController = client.memberListController(query: query)
memberListController.synchronize()

// query channel moderators
let query = ChannelMemberListQuery(cid: channelId, filter: .equal(.isModerator, to: true))
let memberListController = client.memberListController(query: query)
memberListController.synchronize()

// query for banned members in channel
let query = ChannelMemberListQuery(cid: channelId, filter: .equal(.banned, to: true))
let memberListController = client.memberListController(query: query)
memberListController.synchronize()

// query members who joined the channel directly or accepted an invite
let query = ChannelMemberListQuery(cid: channelId, filter: .equal(.joined, to: true))
let memberListController = client.memberListController(query: query)
memberListController.synchronize()

// query members who have rejected invite or have pending invite
let query = ChannelMemberListQuery(cid: channelId, filter: .equal(.joined, to: false))
let memberListController = client.memberListController(query: query)
memberListController.synchronize()

// you can also query members by custom data by providing a string directly
let query = ChannelMemberListQuery(cid: channelId, filter: .equal("subscription", to: "gold_plan"))
let memberListController = client.memberListController(query: query)
memberListController.synchronize()

Query Parameters

nametypedescriptiondefaultoptional
filtersobjectQuery filters to use. You can query on any of the custom fields defined above{}
sortobjectSort parameters{ created_at:1 }
optionsobjectPagination options{ limit:100, offset:0 }

By default when query members does not have any filter and it will match all members on your channel.

Member Queryable Built-In Fields

The following fields can be used to filter channel members, along with any custom data associated with them:

NameTypeDescriptionSupported operators
idstringUser ID$eq - $in
namestringUser name$eq - $in - $autocomplete - $q
channel_rolestringMember role$eq
bannedbooleanBan status$eq
invitestring accepted values: - pending - accepted - rejectedInvite status$eq
joinedbooleanWhether user joined the channel or not$eq
created_atstring (RFC3339)Time when the member was created$eq - $gt - $gte - $lt - $lte
updated_atstring (RFC3339)Time when the member was updated$eq - $gt - $gte - $lt - $lte
last_activestring (RFC3339)Last time the member was active$eq - $gt - $gte - $lt - $lte
cidstringChannel CID$eq
user.emailstringUser’s email property$eq - $in - $autocomplete

Query options

nametypedescriptiondefaultoptional
limitintegerNumber of members to return100
offsetintegerOffset (max is 1000)0
user_id_ltstringPagination option: excludes members with ID greater or equal the value
user_id_ltestringPagination option: excludes members with ID greater than the value
user_id_gtstringPagination option: excludes members with ID less or equal the value
user_id_gtestringPagination option: excludes members with ID less than the value
created_at_afterstringPagination option: select members created after the date (RFC399)
created_at_beforestringPagination option: select members created before the date (RFC399)
created_at_before_or_equalstringPagination option: select members created before or equal the date (RFC399)
created_at_after_or_equalstringPagination option: select members created after or equal the date (RFC399)

Response

Field nameDescription
MembersThe list of members matching the query
© Getstream.io, Inc. All Rights Reserved.