Membership Metadata

Manage membership with BizOps Workspace

You can manage user-channel membership using BizOps Workspace on Admin Portal. It lets you add, update, or remove users from channels on your apps' keysets.

Besides users and channels, you can store the relationship between them as memberships. When you add a user to a channel, the user becomes a member of that channel.

Memberships and subscriptions

Adding channel memberships is not the same as subscribing to a channel. Memberships manage relationships and metadata. To receive messages, subscribe.

You don’t need to persist membership information to send or receive messages. Persisting memberships helps when you want to track members of a channel, show a user’s channel list, or let a user join or leave channels.

When a user joins channels in your app, the user changes their memberships. Membership methods take the current client’s User ID by default, unless you explicitly specify another user’s ID.

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.

PubNub emits events to notify clients when users are added to or removed from a channel. You can enable this feature using the Admin Portal. In the App Context section, select or clear the appropriate checkboxes to enable or disable sending particular events.

Illuminate & sensitive data

You can capture and track your App Context data in Illuminate for real-time decisioning and analytics. Illuminate captures all data you define with JSON paths and map when creating measures and dimensions for the Business Objects. To protect privacy, avoid placing any PII (email address, profile URL, or IP address) in the custom fields of your App Context mappings.

The App Context service allows you to perform the following operations on memberships:

Channel memberships

OperationDescription
Set channel memberships
Adds the current user to one or more channels or updates the custom metadata for the existing memberships. This operation creates a membership for the user to the specified channels.
Remove channel memberships
Removes the current user from one or more channels. This operation removes a membership for the user from the specified channels.
Get channel memberships
Returns a list of channel memberships for the current user.

Set channel memberships

You can add a single user to one or more channels, creating memberships, or update custom metadata on existing memberships. Provide custom metadata as key-value pairs.

API limits

To learn about the maximum length of parameters used to set channel membership metadata, refer to REST API docs.

The example below adds the current user to the channels my_channel and my_channel_2 and adds the starred metadata to the newly created my_channel_2 membership.

pubnub.objects.setMemberships({
channels: [
"channel-1",
{id: "channel-2", custom: {starred: true}
}]
});

On success, the SDK returns channel data for the specified channels with HTTP 200. It also emits the objects -> membership -> set event that other clients can consume.

Referential integrity

Enabling referential integrity on your app’s keyset in the Admin Portal ensures that memberships can only be created for existing users and channels, and automatically deletes memberships when their associated user or channel is deleted.

If it’s not enabled, memberships can be created even for the non-existent user and channel entities, while deleting a user or channel entity does not automatically delete any associated membership objects.

Remove channel memberships

You can remove a single user from one or more channels, deleting the membership relationship. The example below removes the current user from the channel my_channel_2.

pubnub.objects.removeMemberships({
channels: ["my_channel_2"]
});

On completion, the SDK emits the objects -> membership -> delete event so it can be consumed for other clients (users). Refer to the Receive Messages to learn more.

Get channel memberships

You can retrieve a list of channel memberships for the current user. The following example returns all memberships for the current user.

pubnub.objects.getMemberships();

On success, the SDK returns all channels for which the user is a member with HTTP 200.

Channel member

Apart from users and channels, PubNub also allows you to store the relations between them called memberships. Each user that you add to a channel becomes a member of that specific channel.

The following section describes how to add channel memberships from an external user's perspective, that is, when another user wants to add/remove members to a channel. This is the case when a user already is a member (or a moderator) of a specific channel and wishes to add/remove users to that channel. Setting members in a channel is also a more efficient way of handling bulk operations than setting memberships.

PubNub emits events to notify clients when users are added to or removed from a channel. You can enable this feature using the Admin Portal. In the App Context section, select or clear the appropriate checkboxes to enable or disable sending particular events.

The App Context service allows you to perform the following operations on channel 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.

OperationDescription
Set members in channel
Adds one or more users to a single channel. This operation creates memberships for these users to the specified channel.
Remove members from channel
Removes one or more users from a single channel. This operation removes memberships for these users from the specified channel.
Get members in channel
Returns a list of users on a single channel. The list includes user's custom metadata if available and includes only User IDs for users who do not have custom metadata.

Set members in channel

You can add one or more users to a single channel (effectively creating a membership relation between the users and the channel) or update the custom metadata of one or more users that are existing members of a single channel. To update custom metadata of existing memberships, provide the desired information as key/value pairs.

API limits

To learn about the maximum length of parameters used to set channel members metadata, refer to REST API docs.

The code below adds the membership to my_channel for the users johndoe_1 and janedoe_1. Additionally, custom metadata trialPeriod is also added to the newly created membership for janedoe_1.

pubnub.objects.setChannelMembers({
channel: "my_channel",
uuids: [
'johndoe_1',
{ id: 'janedoe_1', custom: { trialPeriod: false } },
],
});

On success, the SDK will return the channel data of the specified channel with HTTP 200. It will also fire the objects -> membership -> set event so it can be consumed for other clients (users). Refer to the Receive Messages to learn more.

Referential integrity

Enabling referential integrity on your app’s keyset in the Admin Portal ensures that memberships can only be created for existing users and channels, and automatically deletes memberships when their associated user or channel is deleted.

If it’s not enabled, memberships can be created even for the non-existent user and channel entities, while deleting a user or channel entity does not automatically delete any associated membership objects.

Remove members from channel

You can remove one or more users from a single channel by providing the channel ID and a list of users. The following example deletes the membership to my_channel_2 for the users johndoe_1 and janedoe_1.

pubnub.objects.removeChannelMembers({
channel: "my_channel_2",
uuids: ["johndoe_1", "janedoe_1"]
}
);

On completion, the SDK emits objects -> membership -> delete.

Get members in channel

You can retrieve a list of members of a single channel by providing its ID. The following example returns the members of the channel my_channel.

pubnub.objects.getChannelMembers({
channel: "my_channel"
});

On success, the PubNub SDK returns the UUIDs of all the users, and the associated channel metadata for that channel, along with HTTP 200.

Last updated on