Set, get, and remove memberships
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 user's channel list from the user's side: add the user to channels, read their current list, and remove them from channels. App Context must be enabled on the keyset before you call it, and new keysets don't enable it by default. This is the memberships direction of a membership, which starts from the user. To manage the same relationship from a channel's side instead, refer to Set, get, and remove members.
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 memberships it can change at once. Refer to App Context API limits for the current maximum.
Add a user to channels
Call Set Memberships with a list of channels to add a User ID to them, creating a membership record for each. If you omit the User ID, the call applies to the client's own User ID. Pass an object instead of a plain channel-name string to attach custom fields to that membership record.
- JavaScript
- Java
- Kotlin
- C#
- Go
- Swift
- PHP
- Unity
1
1
1
1
1
1
1
1
If Access Manager is enabled, this call requires both a join permission grant on every channel and an update permission grant on the User ID, in the same token. This is more than setting members from the channel's side needs, which requires only a channel-level grant. 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, the User ID and every channel need their own metadata record before you can add the membership. Leaving it off lets you add a membership for a User ID or channel that has no metadata record yet.
Get a user's memberships
Call Get Memberships to list a User ID's current channels. If you omit the User ID, the call applies to the client's own User ID. Pass include.channelFields to receive each channel's full metadata alongside the membership's own custom fields, instead of only the channel name.
- 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 User ID. 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 channel.* fields a memberships 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 a user from channels
Call Remove Memberships with a list of channels to remove the User ID's membership to them.
- JavaScript
- Java
- Kotlin
- C#
- Go
- Swift
- PHP
- Unity
1
1
1
1
1
1
1
1
If Access Manager is enabled, this call requires both a join permission grant on every channel and an update permission grant on the User ID, in the same token. That's the same pairing Add a user to channels needs. On success, the call also fires the membership delete App Context event other subscribers receive. Removing a membership deletes the membership record. It doesn't delete the user's or the channel's own metadata.
Manage a user's memberships without code
BizOps Workspace's User Management module manages the same channel list from the Admin Portal, with no SDK call required:
- Add membership. Assign the user to one or more channels.
- Update membership. Change a membership's custom fields.
- Delete membership. Remove one membership, 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 channel you add here also appears when you get that channel's members.
Related tasks
- App Context. The two directions of a membership and how they relate to Presence.
- Set, get, and remove members. Manage the same relationship from a channel's side.
- Set, get, and remove user metadata. Give this user their own name, email, and custom fields.
- App Context filtering. Filter a memberships list by
channel.*fields. - App Context API limits. Memberships per user and memberships per write.