Presence API for Swift Native SDK
Presence lets you track who is online or offline and store custom state information. Presence shows:
- When a user has joined or left a channel
- How many users are subscribed to a particular channel (occupancy)
- Which channels a user or device is subscribed to
- Presence state associated with these users
Learn more about our Presence feature in the Presence overview.
Here now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
This method returns information about the current state of a channel, including a list of unique user IDs (universally unique identifiers, UUIDs) currently subscribed to the channel and the total occupancy count of the channel.
Cache
This method has a 3-second response cache time.
Method(s)
To call Here Now you can use the following method(s) in the Swift SDK:
1func hereNow(
2 on channels: [String],
3 and groups: [String] = [],
4 includeUUIDs: Bool = true,
5 includeState: Bool = false,
6 custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
7 completion: ((Result<[String: PubNubPresence], Error>) -> Void)?
8)
| Parameter | Description |
|---|---|
on *Type: [String] Default: n/a | The list of channels to return occupancy results from. |
andType: [String] Default: [] | The list of channel groups for which will return occupancy results from. Wildcards are not supported. |
includeUUIDsType: Bool Default: true | Setting uuid to false disables the return of UUIDs. |
includeStateType: Bool Default: false | Setting this flag to true will return the subscribe state information as well. |
customType: PubNub.RequestConfigurationDefault: PubNub.RequestConfiguration() | An object that allows for per-request customization of PubNub Configuration or Network Session. |
completionType: ((Result<[String: PubNubPresence], Error>) -> Void)?Default: nil | The async Result of the method call. |
Completion handler result
Success
A Dictionary of channels mapped to their respective PubNubPresence
1public protocol PubNubPresence {
2
3 /// The channel identifier
4 var channel: String { get }
5
6 /// The total number of UUIDs present on the channel
7 var occupancy: Int { get set }
8
9 /// The known UUIDs present on the channel
10 ///
11 /// The `count` of this Array may differ from the `occupancy` field
12 var occupants: [String] { get set }
13
14 /// The Dictionary of UUIDs mapped to their respective presence state data
15 var occupantsState: [String: JSONCodable] { get set }
show all 16 linesFailure
An Error describing the failure.
Sample code
Get a list of UUIDs subscribed to channel
Reference code
1
Other examples
Return occupancy only
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
You can return only the occupancy information for a single channel by specifying the channel and setting UUIDs to false:
1
Channel group usage
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
1
Where now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
This method returns the list of channels a UUID is subscribed to.
Timeout events
If the app restarts (or the page refreshes) within the heartbeat window, no timeout event is generated.
Method(s)
To call whereNow() you can use the following method(s) in the Swift SDK:
1func whereNow(
2 for uuid: String,
3 custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
4 completion: `((Result<[String: [String]], Error>) -> Void)?`
5)
| Parameter | Description |
|---|---|
for *Type: String Default: n/a | Specifies the UUID to return channel list for. |
customDefault: PubNub.RequestConfiguration() | An object that allows for per-request customization of PubNub configuration or network session. For more information, refer to the Request Configuration section. |
completionType: ((Result<[String: [String]], Error>) -> Void)?Default: nil | The async Result of the method call. |
Completion handler result
Success
A Dictionary of UUIDs mapped to their respective Array of channels they have presence on.
Failure
An Error describing the failure.
Sample code
You simply need to define the uuid and the callback function to be used to send the data to as in the example below.
Get a list of channels a UUID is subscribed to
1
User state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
For information on how to receive presence events and what those events are, refer to Presence Events.
Clients can set a dynamic custom state (score, game state, location) for their users on one or more channels and store it on a channel as long as the user stays subscribed.
The state is not persisted, and when the client disconnects, the state data is lost. For more information, refer to Presence State.
Method(s)
Set state
1func setPresence(
2 state: [String: JSONCodableScalar],
3 on channels: [String],
4 and groups: [String] = [],
5 custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
6 completion: ((Result<JSONCodable, Error>) -> Void)?
7)
| Parameter | Description |
|---|---|
state *Type: [String: JSONCodableScalar] Default: n/a | The state Dictionary to store. Nesting of Dictionary values isn't permitted, and key names beginning with prefix pn are reserved. Setting the state will overwrite the previous values set. Clearing out the state involves passing in an empty Dictionary. |
onType: [String] Default: n/a | The list of channel to set the state on. Pass an empty array to not set. |
andType: [String] Default: n/a | The list of channel groups to set the state on. |
customDefault: PubNub.RequestConfiguration() | An object that allows for per-request customization of PubNub configuration or network session. For more information, refer to the Request Configuration section. |
completionType: ((Result<JSONCodable, Error>) -> Void)?Default: nil | The async Result of the method call. |
Completion handler result
Success
The presence State set as a JSONCodable.
Failure
An Error describing the failure.
Get state
1func getPresenceState(
2 for uuid: String,
3 on channels: [String],
4 and groups: [String] = [],
5 custom requestConfig: PubNub.RequestConfiguration = PubNub.RequestConfiguration(),
6 completion: ((Result<(uuid: String, stateByChannel: [String: JSONCodable]), Error>) -> Void)?
7)
| Parameter | Description |
|---|---|
for *Type: String Default: n/a | The UUID to retrieve the state for. |
onType: [String] Default: n/a | The list of channel to get the state on. Pass an empty array to not get. |
andType: [String] Default: [] | The list of channel groups to get the state on. Pass an empty array to not get. |
customDefault: PubNub.RequestConfiguration() | An object that allows for per-request customization of PubNub configuration or network session. For more information, refer to the Request Configuration section. |
completionType: ((Result<(uuid: String, stateByChannel: [String: JSONCodable]), Error>) -> Void)?Default: nil | The async Result of the method call. |
Completion handler result
Success
A Tuple containing the UUID that set the State and a Dictionary of channels mapped to their respective State.
Failure
An Error describing the failure.
Sample code
Set state
1
Get state
1
Other examples
Converting the response to a JSON dictionary
1
Converting the response to a custom object
1