User Metadata

The Objects feature provides easy-to-use, serverless storage for user metadata, channel metadata, channel memberships, and channel members without the need to stand up an external infrastructure.

Clients can optionally store metadata for users to use them in front-end applications and enhance your application. You can store any of the predefined properties for a user such as name, email, profileURL, externalId. Additionally, you can use a custom property to store any custom attribute for a user. Some examples of custom data are nickname, color etc.

PubNub also generates events when the metadata associated to a particular user is set or deleted. Your application can receive these events in real time and dynamically react to data changes within the app. You can enable these events from the Admin Portal. See how to enable Objects.

Events for each user are published to a channel named for each user. For example, to receive events for the user with UUID chat-user-9A7X8, you would subscribe to the channel named chat-user-9A7X8.

Set User Metadata

You can set any of the predefined or custom user metadata by providing the desired information as key/value pairs. The code below adds the name, email, and custom nickname information to the current user.

pubnub.objects.setUUIDMetadata({
data: {
name: "John Doe",
email: "johndoe@pubnub.com",
custom: {
"nickname": "Mr. Mysterious"
}
}
});

On success, the PubNub SDK will return metadata of the user along with the status 200. It will also fire User Metadata Set event that can be consumed by other clients. Refer to the Receive Messages section to learn more.

Get User Metadata

You can retrieve the metadata of the current user. The code below returns all metadata for the current user.

pubnub.objects.getUUIDMetadata();

On success, the PubNub SDK will return the all metadata of the user along with the status 200.

Get Metadata for All Users

You can also retrieve metadata for all users at once. If you're interested in their custom metadata as well, you can optionally specify whether custom metadata should be included in the response. The code below returns all predefined and custom metadata of all users:

pubnub.objects.getAllUUIDMetadata();

On success, the PubNub SDK will return the all metadata of all users associated with the API key along with the status 200.

Remove User Metadata

You can remove all metadata for a single user. The code below removes all metadata of the current user.

pubnub.objects.removeUUIDMetadata();

On completion, the PubNub SDK will fire object -> uuid -> delete event that can be consumed by other clients. Refer to the Receive Messages document to learn more.