Set, get, and remove members
Starting a new app? Use DataSync
DataSync is the successor to App Context. It does everything App Context does for users, channels, and memberships, and adds typed schemas, partial updates with ETags, field-level access control, and per-class expiry. Refer to How DataSync compares to App Context for a feature-by-feature comparison.
DataSync is currently available to new accounts and to accounts that are not actively using App Context. If your keysets already use App Context, keep using it for now. App Context remains fully supported and these pages stay accurate.
This guide shows you how to manage a channel's user roster from the channel's side: add users as members, read the current roster, and remove members. App Context must be enabled on the keyset before you call it, and new keysets don't enable it by default. This is the members direction of a membership, which starts from the channel. To manage the same relationship from a user's side instead, refer to Set, get, and remove memberships.
User ID / UUID
User ID is also referred to as UUID/uuid in some APIs and server responses but holds the value of the userId parameter you set during initialization.
Platform limits apply: up to 50,000 memberships per user and up to 5,000 members per channel.
A single set or remove call also caps how many members it can change at once. Refer to App Context API limits for the current maximum.
Add members to a channel
Call Set Channel Members with the channel's name and a list of User IDs to add them as members, creating a membership record for each. Pass an object instead of a plain ID string to attach custom fields to that member's membership record. Setting members this way is more efficient for bulk adds than setting memberships one user at a time.
- JavaScript
- Java
- Kotlin
- C#
- Go
- Swift
- PHP
- Unity
1
1
1
1
1
1
1
1
If Access Manager is enabled, this call requires only a manage permission grant on the channel. Setting a membership from the user's side also needs a grant on the User ID. Refer to App Context and access control for the full permission model. On success, the call also fires the membership set App Context event other subscribers receive.
Turning on Enforce referential integrity for memberships in the Admin Portal changes what this call allows. With it on, every User ID and the channel need their own metadata record before you can add a member. Leaving it off lets you add a member for a User ID or channel that has no metadata record yet.
Get a channel's members
Call Get Channel Members with the channel's name to list its current members. Pass include.UUIDFields to receive each member's full user metadata alongside their custom membership fields, instead of only their User ID.
- JavaScript
- Java
- Kotlin
- C#
- Go
- Swift
- PHP
- Unity
1
1
1
1
1
1
1
1
If Access Manager is enabled, this call requires only a get permission grant on the channel. This call returns a list, so it accepts the same filter, sort, and page parameters as Get metadata for all channels. Refer to App Context filtering for the uuid.* fields a members call accepts.
Every paginated App Context call takes the same three parameters. limit caps how many records come back in one call. It defaults to 100, and 100 is also the maximum. page.next and page.prev are opaque cursor strings copied from a previous response: pass the one you want to continue from, and PubNub returns the adjacent page in that direction. If you supply both, PubNub uses next and ignores prev.
Remove members from a channel
Call Remove Channel Members with the channel's name and a list of User IDs to remove their membership to that channel.
- JavaScript
- Java
- Kotlin
- C#
- Go
- Swift
- PHP
- Unity
1
1
1
1
1
1
1
1
If Access Manager is enabled, this call requires only a manage permission grant on the channel. On success, the call also fires the membership delete App Context event other subscribers receive. Removing a member deletes the membership record. It doesn't delete the user's or the channel's own metadata.
Manage a channel's members without code
BizOps Workspace's Channel Management module manages the same roster from the Admin Portal, with no SDK call required:
- Add membership. Assign one or more users to a channel.
- Update membership. Change a member's custom membership fields.
- Delete membership. Remove one member, or several at once.
A membership you create, edit, or delete in BizOps Workspace is the same record your SDK calls read and write. Changes in either place are immediately visible in the other, and from either direction: a member you add here also appears when you get that user's memberships.
Related tasks
- App Context. The two directions of a membership and how they relate to Presence.
- Set, get, and remove memberships. Manage the same relationship from a user's side.
- Set, get, and remove channel metadata. Give this channel its own name, description, and custom fields.
- App Context filtering. Filter a members list by
uuid.*fields. - App Context API limits. Members per channel and members per write.