Presence API for PubNub Rust SDK

Presence enables you to track the online and offline status of users and devices in real time and store custom state information. Presence provides authoritative information on:

  • When a user has joined or left a channel
  • Who, and how many, users are subscribed to a particular channel
  • Which channel(s) an individual user is subscribed to
  • Associated state information for these users

Learn more about our Presence feature here.

icon

Available in features

fullpresence

Here Now

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

You can obtain information about the current state of a channel including a list of unique user IDs currently subscribed to the channel and the total occupancy count of the channel by calling the here_now() function in your application.

Cache

This method has a 3 second response cache time.

Method(s)

To call here_now(), use the following method in the Rust SDK:

pubnub
.here_now()
.channels(Vec<String>)
.channel_groups(Vec<String>)
.include_state(bool)
.include_user_ids(bool)
.execute()
ParameterTypeRequiredDefaultDescription
channels()Vec<String>Either channels() or channel_groups() is required.The channels to get the details of.
channel_groups()Vec<String>Either channels() or channel_groups() is required.The channel groups to get the details of.
include_state()boolOptionalfalseIf true, the response will include the presence states of the users for channels/channelGroups.
include_user_ids()boolOptionaltrueIf true, the response will include the user IDs of the connected clients.

Basic Usage

Get a list of channels, their occupancy, and individual occupants' states


Returns

The here_now() operation returns a HereNowResult, which contains the following fields:

FieldTypeDescription
total_channelsu32Total number of channels.
total_occupancyu32Number of all users in the provided channels.
channelsVec<HereNowChannel>A vector with values of HereNowChannel for each channel. Refer to HereNowChannel for more details.

HereNowChannel

FieldTypeDescription
nameStringChannel name.
occupancyu32Number of all users in the channel.
occupantsVec<HereNowUser>A vector of HereNowUser, refer to HereNowUser for more details.

HereNowUser

FieldTypeDescription
user_idStringId of the user.
stateserde_json::ValueState of the user. If you're not using the default serde serialization framework, this field is of type Vec<u8>.

Where Now

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

You can obtain information about the current list of channels to which a user ID is subscribed to by calling the where_now() function in your application.

Timeout events

If the app is killed/crashes and restarted (or the page containing the PubNub instance is refreshed on the browser) within the heartbeat window no timeout event is generated.

Method(s)

To call where_now(), use the following method in the Rust SDK:

pubnub
.where_now()
.user_id(String)
.execute()
ParameterTypeRequiredDefaultDescription
user_id()StringOptionalUser ID provided in PubNub configUser ID to get the current channel subscriptions for.

Basic Usage


Returns

The where_now() operation returns a WhereNowResult, which contains the following fields:

FieldTypeDescription
channelsVec<String>List of channels where the user ID is present.

User State

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

The state API is used to set/get key/value pairs specific to a subscriber user ID.

State information is supplied as a JSON object of key/value pairs.

Presence state format

Presence state must be expressed as a JSON object.

Method(s)

To manage presence state, use any of the following method(s) in the Rust SDK:

Set State

pubnub
.set_presence_state(T: Serialize)
.channels(Vec<String>)
.channel_groups(Vec<String>)
.user_id(String)
.execute()
ParameterTypeRequiredDefaultDescription
set_presence_state()T: SerializeYesThe state to set expressed as a JSON object.
channels()Vec<String>Either channels() or channel_groups() is required.Channels to set state on.
channel_groups()Vec<String>Either channels() or channel_groups() is required.Channel groups to set state on.
user_id()StringOptionalUser ID provided in PubNub configThe user ID to set state for.

Get State

pubnub
.get_presence_state()
.channels(Vec<String>)
.channel_groups(Vec<String>)
.user_id(String)
.execute()
ParameterTypeRequiredDefaultDescription
channels()Vec<String>Either channels() or channel_groups() is required.Channels to get state of.
channel_groups()Vec<String>Either channels() or channel_groups() is required.Channel groups to set state of.
user_id()StringOptionalUser ID provided in PubNub configThe user ID to get state for.

Basic Usage


Returns

The set_presence_state() operation returns a SetStateResult which contains the following fields:

FieldTypeDescription
channelStringThe channel the state was set on.
stateserde_json::ValueState of the user. If you're not using the default serde serialization framework, this field is of type Vec<u8>.

The get_presence_state() operation returns a GetStateResult which contains the following fields:

FieldTypeDescription
stateVec<GetStateInfo>Vector of GetStateInfo objects.

GetStateInfo

The GetStateInfo object contains the following fields:

FieldTypeDescription
channel_nameStringThe channel name the state was set on.
stateserde_json::ValueState of the user. If you're not using the default serde serialization framework, this field is of type Vec<u8>.
Last updated on