App Context API for JavaScript SDK
This page describes App Context (formerly Objects v2). To upgrade from Objects v1, refer to the migration guide.
App Context provides easy-to-use, serverless storage for user and channel data you need to build innovative, reliable, scalable applications. Use App Context to easily store metadata about your application users and channels, and their membership associations, without the need to stand up your own databases.
PubNub also triggers events when object data is changed: set, updated, or removed from the database. At the same time, making a request to set the same data that already exist, doesn't trigger any event. Clients can receive these events in real time and update their front-end application accordingly.
Supported and recommended asynchronous patterns
PubNub supports Callbacks, Promises, and Async/Await for asynchronous JS operations. The recommended pattern is Async/Await and all sample requests in this document are based on it. This pattern returns a status only on detecting an error. To receive the error status, you must add the try...catch
syntax to your code.
User
Get Metadata for All Users
Returns a paginated list of UUID Metadata objects, optionally including the custom data object for each.
Method(s)
To Get All UUID Metadata
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.getAllUUIDMetadata({
include: any,
filter: string,
sort: any,
limit: number,
page: any
})
Parameter | Description |
---|---|
include Type: any Default: n/a | Include respective additional fields in the response. |
totalCount Type: Boolean Default: false | Request totalCount to be included in paginated response. By default, totalCount is omitted. |
customFields Type: Boolean Default: false | Whether to fetch custom fields or not. |
filter Type: string Default: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sort Type: any Default: n/a | Key-value pair of a property to sort by, and a sort direction. Available options are id , name , and updated . Use asc or desc to specify sort direction, or specify null to take the default sort direction (ascending). For example: {name: 'asc'} |
limit Type: number Default: 100 | Number of objects to return in response. Default is 100 , which is also the maximum value. |
page Type: any Default: n/a | Use for pagination. |
next Type: String Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
prev Type: String Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data. Ignored if the next parameter is supplied. |
Basic Usage
Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.
const PubNub = require('pubnub');
// Initialize PubNub with demo keys
const pubnub = new PubNub({
publishKey: 'demo',
subscribeKey: 'demo',
userId: 'myUniqueUserId'
});
// Function to get all UUID metadata
async function getAllUUIDMetadata() {
try {
const result = await pubnub.objects.getAllUUIDMetadata();
console.log('UUID Metadata:', result);
} catch (error) {
show all 21 linesResponse
{
"status": 200,
"data": [
{
"id": "uuid-1",
"name": "John Doe",
"externalId": null,
"profileUrl": null,
"email": "johndoe@pubnub.com",
"custom": null,
"updated": "2019-02-20T23:11:20.893755",
"eTag": "MDcyQ0REOTUtNEVBOC00QkY2LTgwOUUtNDkwQzI4MjgzMTcwCg=="
},
{
"id": "uuid-2",
show all 27 linesGet User Metadata
Returns metadata for the specified UUID, optionally including the custom data object for each.
Method(s)
To Get UUID Metadata
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.getUUIDMetadata({
uuid: string,
include: any
})
Parameter | Description |
---|---|
uuid Type: string Default: current uuid | Unique user identifier. If not supplied then current user's uuid is used. |
include Type: any Default: n/a | Include respective additional fields in the response. |
customFields Type: Boolean Default: true | Whether to fetch custom fields or not. |
Basic Usage
// Using UUID from the config
try {
const result = await pubnub.objects.getUUIDMetadata();
} catch (status) {
console.log("operation failed w/ error:", status);
}
// Using the passed in UUID
try {
const result = await pubnub.objects.getUUIDMetadata({
uuid: "myUuid",
});
} catch (status) {
console.log("operation failed w/ error:", status);
}
Response
{
"status": 200,
"data": {
"id": "uuid-1",
"name": "John Doe",
"externalId": null,
"profileUrl": null,
"email": "johndoe@pubnub.com",
"updated": "2019-02-20T23:11:20.893755",
"eTag": "MDcyQ0REOTUtNEVBOC00QkY2LTgwOUUtNDkwQzI4MjgzMTcwCg=="
}
}
Set User Metadata
Unsupported partial updates of custom metadata
The value of the custom metadata parameter sent in this method always overwrites the value stored on PubNub servers. If you want to add new custom data to an existing one, you must:
- Get the existing metadata and store it locally.
- Append the new custom metadata to the existing one.
- Set the entire updated custom object.
Set metadata for a UUID in the database, optionally including the custom data object for each.
Method(s)
To Set UUID Metadata
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.setUUIDMetadata({
uuid: string,
data: any,
include: any,
ifMatchesEtag: string
})
Parameter | Description |
---|---|
uuid Type: string Default: current uuid | Unique user identifier. If not supplied then current user's uuid is used. |
data *Type: any Default: n/a | JSON object with uuid metadata to set. |
name Type: string Default: n/a | Display name for the user. |
externalId Type: string Default: n/a | User's identifier in an external system |
profileUrl Type: string Default: n/a | The URL of the user's profile picture |
email Type: string Default: n/a | The user's email address. |
custom Type: any Default: n/a | JSON providing custom data about the user. Values must be scalar only; arrays or objects are not supported. Filtering App Context data through the custom property is not recommended in SDKs. |
include Type: any Default: n/a | Include respective additional fields in the response. |
customFields Type: boolean Default: true | Whether to fetch custom fields or not. |
ifMatchesEtag Type: string Default: n/a | The entity tag to be used to ensure updates only happen if the object hasn't been modified since it was read. Use the eTag you received from an applicable get metadata method to check against the server entity tag. If the eTags don't match, an HTTP 412 error is thrown. |
API limits
To learn about the maximum length of parameters used to set user metadata, refer to REST API docs.
Basic Usage
// Using UUID from the config
try {
const result = await pubnub.objects.setUUIDMetadata({
data: {
name: "John Doe",
},
});
} catch (status) {
console.log("operation failed w/ error:", status);
}
// Using the passed in UUID
try {
const result = await pubnub.objects.setUUIDMetadata({
uuid: "myUuid",
show all 20 linesResponse
{
"status": 200,
"data": {
"id": "uuid-1",
"name": "John Doe",
"externalId": null,
"profileUrl": null,
"email": "johndoe@pubnub.com",
"updated": "2019-02-20T23:11:20.893755",
"eTag": "MDcyQ0REOTUtNEVBOC00QkY2LTgwOUUtNDkwQzI4MjgzMTcwCg=="
}
}
Remove User Metadata
Removes the metadata from a specified UUID.
Method(s)
To Remove UUID Metadata
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.removeUUIDMetadata({
uuid: string
})
Parameter | Description |
---|---|
uuid Type: string Default: current uuid | Unique user identifier. If not supplied then current user's uuid is used. |
Basic Usage
// Using UUID from the config
try {
const result = await pubnub.objects.removeUUIDMetadata();
} catch (status) {
console.log("operation failed w/ error:", status);
}
// Using the passed in UUID
try {
const result = await pubnub.objects.removeUUIDMetadata({
uuid: "myUuid",
});
} catch (status) {
console.log("operation failed w/ error:", status);
}
Response
{
"status": 0,
"data": {}
}
Channel
Get Metadata for All Channels
Returns a paginated list of Channel Metadata objects, optionally including the custom data object for each.
Method(s)
To Get All Channel Metadata
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.getAllChannelMetadata({
include: any,
filter: string,
sort: any,
limit: number,
page: any
})
Parameter | Description |
---|---|
include Type: any Default: n/a | Include respective additional fields in the response. |
totalCount Type: Boolean Default: false | Request totalCount to be included in paginated response. By default, totalCount is omitted. |
customFields Type: Boolean Default: false | Whether to fetch custom fields or not. |
filter Type: string Default: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sort Type: any Default: n/a | Key-value pair of a property to sort by, and a sort direction. Available options are id , name , and updated . Use asc or desc to specify sort direction, or specify null to take the default sort direction (ascending). For example: {name: 'asc'} |
limit Type: number Default: 100 | Number of objects to return in response. Default is 100 , which is also the maximum value. |
page Type: any Default: n/a | Use for pagination. |
next Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
prev Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data. Ignored if the next parameter is supplied. |
Basic Usage
// Get the total number of channels
try {
const result = await pubnub.objects.getAllChannelMetadata({
include: {
totalCount: true,
},
});
} catch (status) {
console.log("operation failed w/ error:", status);
}
// Get all channels that have IDs starting with "pro."
try {
const result = await pubnub.objects.getAllChannelMetadata({
filter: 'name LIKE "*Team"',
show all 19 linesResponse
{
"status": 200,
"data": [
{
"id": "team.blue",
"name": "Blue Team",
"description": "The channel for Blue team and no other teams.",
"custom": null,
"updated": "2019-02-20T23:11:20.893755",
"eTag": "RTc1NUQwNUItREMyNy00Q0YxLUJCNDItMEZDMTZDMzVCN0VGCg=="
},
{
"id": "team.red",
"name": "Red Team",
"description": "The channel for Red team and no other teams.",
show all 35 linesGet Channel Metadata
Returns metadata for the specified Channel, optionally including the custom data object for each.
Method(s)
To Get Channel Metadata
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.getChannelMetadata({
channel: string,
include: any
})
Parameter | Description |
---|---|
channel *Type: string Default: n/a | Channel name. |
include Type: any Default: n/a | Include respective additional fields in the response. |
customFields Type: Boolean Default: true | Whether to fetch custom fields or not. |
Basic Usage
try {
const result = await pubnub.objects.getChannelMetadata({
// `channel` is the `id` in the _metadata_, not `name`
channel: "team.blue",
});
} catch (status) {
console.log("operation failed w/ error:", status);
}
Response
{
"status": 200,
"data": {
"id": "team.blue",
"name": "Blue Team",
"description": "The channel for Blue team and no other teams.",
"updated": "2019-02-20T23:11:20.893755",
"eTag": "RTc1NUQwNUItREMyNy00Q0YxLUJCNDItMEZDMTZDMzVCN0VGCg=="
}
}
Set Channel Metadata
Unsupported partial updates of custom metadata
The value of the custom metadata parameter sent in this method always overwrites the value stored on PubNub servers. If you want to add new custom data to an existing one, you must:
- Get the existing metadata and store it locally.
- Append the new custom metadata to the existing one.
- Set the entire updated custom object.
Set metadata for a Channel in the database, optionally including the custom data object for each.
Method(s)
To Set Channel Metadata
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.setChannelMetadata({
channel: string,
data: any,
include: any,
ifMatchesEtag: string
})
Parameter | Description |
---|---|
channel *Type: string Default: n/a | Channel name. |
data *Type: any Default: n/a | JSON object with channel metadata to set. |
name Type: string Default: n/a | Name of a channel. |
description Type: string Default: n/a | Description of a channel. |
custom Type: any Default: n/a | JSON providing custom data about the channel. Values must be scalar only; arrays or objects are not supported. App Context filtering language doesn’t support filtering by custom properties. |
include Type: any Default: n/a | Include respective additional fields in the response. |
customFields Type: boolean Default: true | Whether to fetch custom fields or not. |
ifMatchesEtag Type: string Default: n/a | The entity tag to be used to ensure updates only happen if the object hasn't been modified since it was read. Use the eTag you received from an applicable get metadata method to check against the server entity tag. If the eTags don't match, an HTTP 412 error is thrown. |
API limits
To learn about the maximum length of parameters used to set channel metadata, refer to REST API docs.
Basic Usage
try {
const result = await pubnub.objects.setChannelMetadata({
channel: "team.red",
data: {
name: "Red Team",
description: "The channel for Red team and no other teams.",
custom: {
owner: "Red Leader",
},
},
include: {
customFields: false,
},
});
} catch (status) {
show all 17 linesResponse
{
"status": 200,
"data": {
"id": "team.red",
"name": "Red Team",
"description": "The channel for Blue team and no other teams.",
"updated": "2019-02-20T23:11:20.893755",
"eTag": "RTc1NUQwNUItREMyNy00Q0YxLUJCNDItMEZDMTZDMzVCN0VGCg=="
}
}
Other Examples
Iteratively update existing metadata
import PubNub from 'pubnub';
const pubnub = new PubNub({
publishKey: 'demo',
subscribeKey: 'demo',
userId: 'example'
});
const channel = 'team.red';
const name = 'Red Team';
const description = 'The channel for Red team.';
const customField = { visible: 'team' };
// Function to set and then update channel metadata
const updateChannelMetadata = async () => {
show all 57 linesRemove Channel Metadata
Removes the metadata from a specified channel.
Method(s)
To Remove Channel Metadata
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.removeChannelMetadata({
channel: string
})
Parameter | Description |
---|---|
channel *Type: String Default: n/a | Channel name. |
Basic Usage
try {
const result = await pubnub.objects.removeChannelMetadata({
channel: "team.red",
});
} catch (status) {
console.log("operation failed w/ error:", status);
}
Response
{
"status": 0,
"data": {}
}
Channel Memberships
Get Channel Memberships
The method returns a list of channel memberships for a user. This method doesn't return a user's subscriptions.
Method(s)
To Get Memberships
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.getMemberships({
uuid: string,
include: any,
filter: string,
sort: any,
limit: number,
page: any
})
Parameter | Description |
---|---|
uuid Type: string Default: current uuid | Unique user identifier. If not supplied then current user's uuid is used. |
include Type: any Default: n/a | Include respective additional fields in the response. |
totalCount Type: boolean Default: false | Request totalCount to be included in paginated response. By default, totalCount is omitted. |
customFields Type: boolean Default: false | Whether to include custom fields in the response. |
channelFields Type: boolean Default: false | Whether to include fields for channel metadata in the response. |
customChannelFields Type: boolean Default: false | Whether to include custom fields for channel metadata in the response. |
statusField Type: boolean Default: false | Whether to include the membership's status field in the response. |
channelStatusField Type: boolean Default: false | Whether to include the channel's status field in the response. |
channelTypeField Type: boolean Default: false | Whether to include channel's type fields in the response. |
filter Type: string Default: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sort Type: any Default: n/a | Key-value pair of a property to sort by, and a sort direction. Available options are updated , status , type , channel.id , channel.name , channel.updated , channel.status and, channel.type . Use asc or desc to specify sort direction, or specify null to take the default sort direction (ascending). For example: {channel.name: 'asc'} |
limit Type: number Default: 100 | Number of objects to return in response. Default is 100 , which is also the maximum value. |
page Type: any Default: n/a | Use for pagination. |
next Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
prev Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data. Ignored if the next parameter is supplied. |
Basic Usage
// Using UUID from the config
try {
const result = await pubnub.objects.getMemberships();
} catch (status) {
console.log("operation failed w/ error:", status);
}
// Using the passed in UUID
try {
const result = await pubnub.objects.getMemberships({
uuid: "myUuid",
include: {
channelFields: true,
},
});
show all 28 linesResponse
{
"status": 200,
"data": [
{
"channel": {
"id": "my-channel",
"name": "My channel",
"description": "A channel that is mine",
"custom": null,
"updated": "2019-02-20T23:11:20.893755",
"eTag": "RTc1NUQwNUItREMyNy00Q0YxLUJCNDItMEZDMTZDMzVCN0VGCg=="
},
"custom": {
"starred": false
},
show all 38 linesSet Channel Memberships
Set channel memberships for a UUID.
Method(s)
To Set Memberships
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.setMemberships({
uuid: string,
channels: Array<string>,
include: any,
filter: string,
sort: any,
limit: number,
page: any
})
Parameter | Description |
---|---|
uuid Type: string Default: current uuid | Unique user identifier. If not supplied then current user's uuid is used. |
channels *Type: Array <string> Default: n/a | Array of channels to add to membership. Array can contain strings (channel-name only) or objects which can include custom data, like status or type, for example: { id: "my-channel-3", custom: { owner: "PubNubUser" }, type: "regular_membership", status: "active" } . |
include Type: any Default: n/a | Include respective additional fields in the response. |
totalCount Type: boolean Default: false | Request totalCount to be included in paginated response. By default, totalCount is omitted. |
customFields Type: boolean Default: false | Whether to include custom fields in the response. |
statusField Type: boolean Default: false | Whether to include the membership's status field in the response. |
typeField Type: boolean Default: false | Whether to include the membership's type field in the response. |
channelFields Type: boolean Default: false | Whether to include fields for channel metadata in the response. |
customChannelFields Type: boolean Default: false | Whether to include custom fields for channel metadata in the response. |
channelStatusField Type: boolean Default: false | Whether to include the channel's status field in the response. |
channelTypeField Type: boolean Default: false | Whether to include channel's type fields in the response. |
filter Type: String Default: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sort Type: any Default: n/a | Key-value pair of a property to sort by, and a sort direction. Available options are updated , status , type , channel.id , channel.name , channel.updated , channel.status and, channel.type . Use asc or desc to specify sort direction, or specify null to take the default sort direction (ascending). For example: {channel.name: 'asc'} |
limit Type: Number Default: 100 | Number of objects to return in response. Default is 100 , which is also the maximum value. |
page Type: any Default: n/a | Use for pagination. |
next Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
prev Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data. Ignored if the next parameter is supplied. |
API limits
To learn about the maximum length of parameters used to set channel membership metadata, refer to REST API docs.
Basic Usage
// Using UUID from the config
try {
const result = await pubnub.objects.setMemberships({
channels: [
"my-channel",
{ id: "channel-with-status-type", custom: { hello: "World" }, status: 'helloStatus', type:'helloType'}
]
});
} catch (status) {
console.log("operation failed w/ error:", status);
}
// Using the passed in UUID
try {
const result = await pubnub.objects.setMemberships({
show all 28 linesResponse
{
"status": 200,
"data": [
{
"channel": {
"id": "my-channel",
"name": "My channel",
"description": "A channel that is mine",
"custom": null,
"updated": "2019-02-20T23:11:20.893755",
"eTag": "RTc1NUQwNUItREMyNy00Q0YxLUJCNDItMEZDMTZDMzVCN0VGCg=="
},
"custom": {
"starred": false
},
show all 38 linesRemove Channel Memberships
Remove channel memberships for a UUID.
Method(s)
To Remove Memberships
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.removeMemberships({
uuid: string,
channels: Array<string>,
include: any,
filter: string,
sort: any,
limit: number,
page: any
})
Parameter | Description |
---|---|
uuid Type: string Default: current uuid | Unique user identifier. If not supplied then current user's uuid is used. |
channels *Type: Array <string> Default: n/a | Channels to remove from membership. |
include Type: any Default: n/a | Include respective additional fields in the response. |
totalCount Type: boolean Default: false | Request totalCount to be included in paginated response. By default, totalCount is omitted. |
customFields Type: boolean Default: false | Whether to fetch custom fields or not. |
channelFields Type: boolean Default: false | Include fields for channels metadata. |
customChannelFields Type: boolean Default: false | Include custom fields for channels metadata. |
filter Type: string Default: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sort Type: any Default: n/a | Key-value pair of a property to sort by, and a sort direction. Available options are updated , channel.id , channel.name , and channel.updated . Use asc or desc to specify sort direction., or specify null to take the default sort direction (ascending). For example: {channel.name: 'asc'} |
limit Type: Number Default: 100 | Number of objects to return in response. Default is 100 , which is also the maximum value. |
page Type: any Default: n/a | Use for pagination. |
next Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
prev Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data. Ignored if the next parameter is supplied. |
Basic Usage
// Using UUID from the config
try {
const result = await pubnub.objects.removeMemberships({
channels: ["ch-1", "ch-2"],
});
} catch (status) {
console.log("operation failed w/ error:", status);
}
// Using the passed in UUID
try {
const result = await pubnub.objects.removeMemberships({
uuid: "myUuid",
channels: ["ch-1", "ch-2"],
});
show all 18 linesResponse
{
"status": 200,
"data": [
{
"channel": {
"id": "my-channel",
"name": "My channel",
"description": "A channel that is mine",
"custom": null,
"updated": "2019-02-20T23:11:20.893755",
"eTag": "RTc1NUQwNUItREMyNy00Q0YxLUJCNDItMEZDMTZDMzVCN0VGCg=="
},
"custom": {
"starred": false
},
show all 38 linesChannel Members
Get Channel Members
The method returns a list of members in a channel. The list will include user metadata for members that have additional metadata stored in the database.
Method(s)
To Get Channel Members
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.getChannelMembers({
channel: string,
include: any,
filter: string,
sort: any,
limit: number,
page: any
})
Parameter | Description |
---|---|
channel *Type: string Default: n/a | Channel name. |
include Type: any Default: n/a | Include respective additional fields in the response. |
totalCount Type: boolean Default: false | Request totalCount to be included in paginated response. By default, totalCount is omitted. |
customFields Type: boolean Default: false | Whether to fetch custom fields |
UUIDFields Type: boolean Default: false | Whether to include fields for UUIDs metadata. |
customUUIDFields Type: boolean Default: false | Whether to include custom fields for UUIDs metadata. |
statusField Type: boolean Default: false | Whether to include the member's status field in the response. |
UUIDStatusField Type: boolean Default: false | Whether to include the member's status field in the response. |
UUIDTypeField Type: boolean Default: false | Whether to include member's type fields in the response. |
filter Type: String Default: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sort Type: any Default: n/a | Key-value pair of a property to sort by, and a sort direction. Available options are: updated , status , type , uuid.id , uuid.name , uuid.updated , uuid.status , and uuid.type . Use asc or desc to specify sort direction, or specify null to take the default sort direction (ascending). For example: {uuid.name: 'asc'} |
limit Type: number Default: 100 | Number of objects to return in response. Default is 100 , which is also the maximum value. |
page Type: any Default: n/a | Use for pagination. |
next Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
prev Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data. Ignored if the next parameter is supplied. |
Basic Usage
try {
const result = await pubnub.objects.getChannelMembers({
channel: "myChannel",
include: {
UUIDFields: true,
},
});
} catch (status) {
console.log("operation failed w/ error:", status);
}
// Get all channel members with "admin" in the description
try {
const result = await pubnub.objects.getChannelMembers({
channel: "myChannel",
show all 20 linesResponse
{
"status": 200,
"data": [
{
"uuid": {
"id": "uuid-1",
"name": "John Doe",
"externalId": null,
"profileUrl": null,
"email": "jack@twitter.com",
"custom": null,
"updated": "2019-02-20T23:11:20.893755",
"eTag": "MDcyQ0REOTUtNEVBOC00QkY2LTgwOUUtNDkwQzI4MjgzMTcwCg=="
},
"custom": {
show all 41 linesSet Channel Members
This method sets members in a channel.
Method(s)
To Set Channel Members
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.setChannelMembers({
channel: string,
uuids: Array<string>,
include: any,
filter: string,
sort: any,
limit: number,
page: any
})
Parameter | Description |
---|---|
channel *Type: string Default: n/a | Channel name. |
uuids *Type: Array Default: n/a | Array of members to add to the channel . Array can contain strings (uuid only) or objects which can include custom data, for example: { id: "uuid-3", custom: { role: "Super Admin" } } . |
include Type: any Default: n/a | Include respective additional fields in the response. |
totalCount Type: boolean Default: false | Request totalCount to be included in paginated response. By default, totalCount is omitted. |
customFields Type: boolean Default: false | Whether to fetch custom fields. |
statusField Type: boolean Default: false | Whether to include the member's custom status field in the response. |
typeField Type: boolean Default: false | Whether to include the member's custom type field in the response. |
UUIDFields Type: boolean Default: false | Whether to include fields for UUID's metadata. |
customUUIDFields Type: boolean Default: false | Whether to include custom fields for UUIDs metadata. |
UUIDStatusField Type: boolean Default: false | Whether to include the member's status field in the response. |
UUIDTypeField Type: boolean Default: false | Whether to include the member's type field in the response. |
filter Type: string Default: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sort Type: any Default: n/a | Key-value pair of a property to sort by, and a sort direction. Available options are: updated , status , type , uuid.id , uuid.name , uuid.updated , uuid.status , and uuid.type . Use asc or desc to specify sort direction, or specify null to take the default sort direction (ascending). For example: {uuid.name: 'asc'} |
limit Type: number Default: 100 | Number of objects to return in response. Default is 100 , which is also the maximum value. |
page Type: any Default: n/a | Use for pagination. |
next Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
prev Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data. Ignored if the next parameter is supplied. |
API limits
To learn about the maximum length of parameters used to set channel members metadata, refer to REST API docs.
Basic Usage
try {
const result = await pubnub.objects.setChannelMembers({
channel: "myChannel",
uuids: [
"uuid-1",
"uuid-2",
{ id: "uuid-3", custom: { role: "Super Admin" } },
],
});
} catch (status) {
console.log("operation failed w/ error:", status);
}
Response
{
"status": 200,
"data": [
{
"uuid": {
"id": "uuid-1",
"name": "John Doe",
"externalId": null,
"profileUrl": null,
"email": "johndoe@pubnub.com",
"custom": null,
"updated": "2019-02-20T23:11:20.893755",
"eTag": "MDcyQ0REOTUtNEVBOC00QkY2LTgwOUUtNDkwQzI4MjgzMTcwCg=="
},
"custom": {
show all 41 linesRemove Channel Members
Remove members from a Channel.
Method(s)
To Remove Channel Members
, you can use the following method(s) in the JavaScript SDK:
pubnub.objects.removeChannelMembers({
channel: string,
uuids: Array<string>,
include: any,
filter: string,
sort: any,
limit: number,
page: any
})
Parameter | Description |
---|---|
channel *Type: string Default: n/a | Channel name. |
uuids *Type: String[] Default: n/a | Members to remove from channel. |
include Type: any Default: n/a | Include respective additional fields in the response. |
totalCount Type: boolean Default: false | Request totalCount to be included in paginated response. By default, totalCount is omitted. |
customFields Type: boolean Default: false | Whether to fetch custom fields or not. |
UUIDFields Type: boolean Default: false | Include fields for UUIDs metadata. |
customUUIDFields Type: boolean Default: false | Include custom fields for UUIDs metadata. |
filter Type: string Default: n/a | Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here. |
sort Type: any Default: n/a | Key-value pair of a property to sort by, and a sort direction. Available options are updated , uuid.id , uuid.name , and uuid.updated . Use asc or desc to specify sort direction, or specify null to take the default sort direction (ascending). For example: {uuid.name: 'asc'} |
limit Type: number Default: 100 | Number of objects to return in response. Default is 100 , which is also the maximum value. |
page Type: any Default: n/a | Use for pagination. |
next Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
prev Type: string Default: n/a | Random string returned from the server, indicating a specific position in a data set. Used for backward pagination, it fetches the previous page, enabling access to earlier data. Ignored if the next parameter is supplied. |
Basic Usage
try {
const result = await pubnub.objects.removeChannelMembers({
channel: "myChannel",
uuids: ["uuid-1", "uuid-2"],
});
} catch (status) {
console.log("operation failed w/ error:", status);
}
Response
{
"status": 200,
"data": [
{
"uuid": {
"id": "uuid-1",
"name": "John Doe",
"externalId": null,
"profileUrl": null,
"email": "johndoe@pubnub.com",
"custom": null,
"updated": "2019-02-20T23:11:20.893755",
"eTag": "MDcyQ0REOTUtNEVBOC00QkY2LTgwOUUtNDkwQzI4MjgzMTcwCg=="
},
"custom": {
show all 41 lines