Channel memberships

PubNub allows you to store the association between users and channels so clients can get channel memberships for a user, or show members in a channel. Memberships are user-channel associations, not subscriptions. Users don't need to join a channel to subscribe and start exchanging messages on it, but it can be a helpful mechanism for your application to use.

Refer to Channel Membership to learn more about working with channel metadata.

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.

Add members to a channel

To add one or more users to a single channel, as you might do when adding a new team channel, add those users to the channel's members with setChannelMembers(), like so:

You can also use setChannelMembers() to update the custom metadata for existing channel members.

pubnub.objects.setChannelMembers({
channel: "channel-1",
uuids: [
"johndoe_1",
{ id: "janedoe_1", custom: { trialPeriod: true } },
],
});

Remove channel members

You can remove one or more users from a single channel by providing the channel ID and a list of users. The code below 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"]
}
);

Add channel memberships

To add one user to several channels, perhaps as part of a new-user onboarding process, add those channels to the user's memberships with setMemberships(), like so:

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

Remove channel memberships

You can remove a single user from one or more channels, effectively deleting a membership relation between the user and the channels. The code below removes the current user from the channel my_channel_2.

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

Membership events

Membership events are generated when memberships are set or removed from the objects database. Object events are disabled by default on new keys. You can enable or disable these events from your key settings in the Admin Portal.

EventDescriptionEvent channel
Membership SetUser-channel association is created or updated.These events are published on {uuid} and {channel}.
Membership RemovedUser-channel association is deleted.These events are published on {uuid} and {channel}.

Membership set event:

{
"channel":"ch-1",
"message":{
"event":"set",
"type":"membership",
"data":{
"channel":{
"id":"ch-1"
},
"uuid":{
"id":"uuid-1"
},
"custom":null,
"updated":"2020-04-17T19:13:59.40962853Z",
"eTag":"AY39mJKK//C0VA"
show all 20 lines

Membership removed event:

{
"channel":"ch-1",
"message":{
"event":"removed",
"type":"membership",
"data":{
"channel":{
"id":"ch-1"
},
"uuid":{
"id":"uuid-1"
},
"custom":null,
"updated":"2020-04-17T19:13:59.40962853Z",
"eTag":"AY39mJKK//C0VA"
show all 20 lines
Last updated on