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 store metadata about your application users and channels, and their membership associations, without running your own databases.

PubNub also triggers events when object data changes: set, update, or removal. Setting the same data again doesn't trigger an 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. Optionally includes Custom.

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
})
* required
ParameterDescription
include
Type: any
Default:
n/a
Whether to include additional fields.
   totalCount
Type: Boolean
Default:
false
Whether to include the total count in the paginated response. Default is false.
   customFields
Type: Boolean
Default:
false
Whether to include the Custom object in the response.
filter
Type: string
Default:
n/a
Filter expression. Only matching objects are returned. See filtering.
sort
Type: any
Default:
n/a
Sort by id, name, updated with asc/desc for sort direction (for example, {name: 'asc'}).
limit
Type: number
Default:
100
Number of objects to return. Default/Max: 100.
page
Type: any
Default:
n/a
Cursor-based pagination.
   next
Type: String
Default:
n/a
Cursor-based pagination.
   prev
Type: String
Default:
n/a
Cursor-based pagination. Ignored if the next parameter is supplied.

Sample code

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.


Response

{
"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 lines

Get 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
})
* required
ParameterDescription
uuid
Type: string
Default:
current uuid
UUID. If not supplied, the current user's UUID is used.
include
Type: any
Default:
n/a
Whether to include additional fields.
   customFields
Type: Boolean
Default:
true
Whether to include the Custom object in the response.

Sample code


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:

  1. Get the existing metadata and store it locally.
  2. Append the new custom metadata to the existing one.
  3. 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
})
* required
ParameterDescription
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
Custom JSON values. Can be strings, numbers, or booleans. Filtering by Custom isn’t supported.
include
Type: any
Default:
n/a
Whether to include additional fields.
   customFields
Type: boolean
Default:
true
Whether to include the Custom object in the response.
ifMatchesEtag
Type: string
Default:
n/a
Use the eTag from an applicable get metadata call to ensure updates only apply if the object hasn’t changed. If the eTags differ, the server returns HTTP 412.
API limits

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

Sample code


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=="
}
}

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
})
* required
ParameterDescription
uuid
Type: string
Default:
current uuid
Unique user identifier. If not supplied then current user's uuid is used.

Sample code


Response

{
"status": 0,
"data": {}
}

Channel

Get metadata for all channels

Returns a paginated list of channel metadata. Optionally includes Custom.

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
})
* required
ParameterDescription
include
Type: any
Default:
n/a
Whether to include additional fields.
   totalCount
Type: Boolean
Default:
false
Whether to include the total count in the paginated response. Default is false.
   customFields
Type: Boolean
Default:
false
Whether to include the Custom object in the response.
filter
Type: string
Default:
n/a
Filter expression. Only matching objects are returned. See filtering.
sort
Type: any
Default:
n/a
Sort by id, name, updated with asc/desc for sort direction (for example, {name: 'asc'}).
limit
Type: number
Default:
100
Number of objects to return. Default/Max: 100.
page
Type: any
Default:
n/a
Cursor-based pagination.
   next
Type: string
Default:
n/a
Cursor-based pagination.
   prev
Type: string
Default:
n/a
Cursor-based pagination. Ignored if the next parameter is supplied.

Sample code


Response

{
"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 lines

Get 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
})
* required
ParameterDescription
channel *
Type: string
Default:
n/a
Channel name.
include
Type: any
Default:
n/a
Whether to include additional fields.
   customFields
Type: Boolean
Default:
true
Whether to include the Custom object in the response.

Sample code


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:

  1. Get the existing metadata and store it locally.
  2. Append the new custom metadata to the existing one.
  3. 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
})
* required
ParameterDescription
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
Custom JSON values. Can be strings, numbers, or booleans. Filtering by Custom isn’t supported.
include
Type: any
Default:
n/a
Whether to include additional fields.
   customFields
Type: boolean
Default:
true
Whether to include the Custom object in the response.
ifMatchesEtag
Type: string
Default:
n/a
Use the eTag from an applicable get metadata call to ensure updates only apply if the object hasn’t changed. If the eTags differ, the server returns HTTP 412.
API limits

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

Sample code


Response

{
"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

Remove 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
})
* required
ParameterDescription
channel *
Type: String
Default:
n/a
Channel name.

Sample code


Response

{
"status": 0,
"data": {}
}

Channel memberships

Get channel memberships

Returns a list of channel memberships for a user. Does not include 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
})
* required
ParameterDescription
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
Whether to include additional fields.
   totalCount
Type: boolean
Default:
false
Whether to include the total count in the paginated response. Default is false.
   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
Filter expression. Only matching objects are returned. See filtering.
sort
Type: any
Default:
n/a
Sort by updated, status, type, channel.id, channel.name, channel.updated, channel.status, channel.type with asc/desc for sort direction (for example, {channel.name: 'asc'}).
limit
Type: number
Default:
100
Number of objects to return. Default/Max: 100.
page
Type: any
Default:
n/a
Cursor-based pagination.
   next
Type: string
Default:
n/a
Cursor-based pagination.
   prev
Type: string
Default:
n/a
Cursor-based pagination. Ignored if the next parameter is supplied.

Sample code


Response

{
"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 lines

Set 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
})
* required
ParameterDescription
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
Whether to include additional fields.
   totalCount
Type: boolean
Default:
false
Whether to include the total count in the paginated response. Default is false.
   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
Filter expression. Only matching objects are returned. See filtering.
sort
Type: any
Default:
n/a
Sort by updated, status, type, channel.id, channel.name, channel.updated, channel.status, channel.type with asc/desc for sort direction (for example, {channel.name: 'asc'}).
limit
Type: Number
Default:
100
Number of objects to return. Default/Max: 100.
page
Type: any
Default:
n/a
Cursor-based pagination.
   next
Type: string
Default:
n/a
Cursor-based pagination.
   prev
Type: string
Default:
n/a
Cursor-based pagination. 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.

Sample code


Response

{
"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 lines

Remove 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
})
* required
ParameterDescription
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
Whether to include additional fields.
   totalCount
Type: boolean
Default:
false
Whether to include the total count in the paginated response. Default is false.
   customFields
Type: boolean
Default:
false
Whether to include the Custom object in the response.
   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
Filter expression. Only matching objects are returned. See filtering.
sort
Type: any
Default:
n/a
Sort by updated, channel.id, channel.name, channel.updated with asc/desc for sort direction (for example, {channel.name: 'asc'}).
limit
Type: Number
Default:
100
Number of objects to return. Default/Max: 100.
page
Type: any
Default:
n/a
Cursor-based pagination.
   next
Type: string
Default:
n/a
Cursor-based pagination.
   prev
Type: string
Default:
n/a
Cursor-based pagination. Ignored if the next parameter is supplied.

Sample code


Response

{
"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 lines

Channel members

Get channel members

Returns a list of channel members. Includes user metadata when available.

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
})
* required
ParameterDescription
channel *
Type: string
Default:
n/a
Channel name.
include
Type: any
Default:
n/a
Whether to include additional fields.
   totalCount
Type: boolean
Default:
false
Whether to include the total count in the paginated response. Default is false.
   customFields
Type: boolean
Default:
false
Whether to include the Custom object in the response.
   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
Filter expression. Only matching objects are returned. See filtering.
sort
Type: any
Default:
n/a
Sort by updated, status, type, uuid.id, uuid.name, uuid.updated, uuid.status, uuid.type with asc/desc for sort direction (for example, {uuid.name: 'asc'}).
limit
Type: number
Default:
100
Number of objects to return. Default/Max: 100.
page
Type: any
Default:
n/a
Cursor-based pagination.
   next
Type: string
Default:
n/a
Cursor-based pagination.
   prev
Type: string
Default:
n/a
Cursor-based pagination. Ignored if the next parameter is supplied.

Sample code


Response

{
"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 lines

Set 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
})
* required
ParameterDescription
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
Whether to include additional fields.
   totalCount
Type: boolean
Default:
false
Whether to include the total count in the paginated response. Default is false.
   customFields
Type: boolean
Default:
false
Whether to include the Custom object in the response.
   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
Filter expression. Only matching objects are returned. See filtering.
sort
Type: any
Default:
n/a
Sort by updated, status, type, uuid.id, uuid.name, uuid.updated, uuid.status, uuid.type with asc/desc for sort direction (for example, {uuid.name: 'asc'}).
limit
Type: number
Default:
100
Number of objects to return. Default/Max: 100.
page
Type: any
Default:
n/a
Cursor-based pagination.
   next
Type: string
Default:
n/a
Cursor-based pagination.
   prev
Type: string
Default:
n/a
Cursor-based pagination. 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.

Sample code


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

Remove 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
})
* required
ParameterDescription
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
Whether to include additional fields.
   totalCount
Type: boolean
Default:
false
Whether to include the total count in the paginated response. Default is false.
   customFields
Type: boolean
Default:
false
Whether to include the Custom object in the response.
   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
Filter expression. Only matching objects are returned. See filtering.
sort
Type: any
Default:
n/a
Sort by updated, uuid.id, uuid.name, uuid.updated with asc/desc for sort direction (for example, {uuid.name: 'asc'}).
limit
Type: number
Default:
100
Number of objects to return. Default/Max: 100.
page
Type: any
Default:
n/a
Cursor-based pagination.
   next
Type: string
Default:
n/a
Cursor-based pagination.
   prev
Type: string
Default:
n/a
Cursor-based pagination. Ignored if the next parameter is supplied.

Sample code


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
Last updated on