Presence API for Cocoa Swift SDK
This SDK has been replaced by a new PubNub Swift SDK written purely in Swift. Check out the Swift SDK overview.
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.
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:
1open func hereNowForChannel(
2 _ channel: String,
3 withCompletion closure: PubNub.PNHereNowCompletionBlock
4)
Parameter | Description |
---|---|
channel *Type: String | Channel name to retrieve the Here Now information. |
closure *Type: PNHereNowCompletionBlock | The completion closure has two arguments: result - in case of successful request processing (data field will contain results of presence audition operation); status - in case if error occurred during request processing (errorData contains error information). |
1open func hereNowForChannel(
2 _ channel: String,
3 withVerbosity level: PNHereNowVerbosityLevel,
4 completion closure: PubNub.PNHereNowCompletionBlock
5)
Parameter | Description |
---|---|
channel *Type: String | Channel name to retrieve the Here Now information. |
level *Type: PNHereNowVerbosityLevel | PNHereNowVerbosityLevel enum filters the response based on the value selected. |
closure *Type: PNHereNowCompletionBlock | The completion closure has two arguments: result - in case of successful request processing (data field will contain results of presence audition operation); status - in case if error occurred during request processing (errorData contains error information). |
Sample code
Get a list of uuids subscribed to channel
1// With .UUID client will pull out list of unique identifiers and occupancy information.
2self.client.hereNowForChannel("my_channel", withVerbosity: .UUID,
3 completion: { (result, status) in
4
5 if status == nil {
6
7 /**
8 Handle downloaded presence information using:
9 result.data.uuids - list of uuids.
10 result.data.occupancy - total number of active subscribers.
11 */
12 }
13 else {
14
15 /**
show all 24 linesResponse
Response objects which is returned by client when Here Now API for channel used:
1open class PNPresenceGlobalHereNowData : PNServiceData {
2
3 // Active channels list.
4 open var channels: [String : [AnyHashable : Any]] { get }
5 // Total number of active channels.
6 open var totalChannels: NSNumber { get }
7 // Total number of subscribers.
8 open var totalOccupancy: NSNumber { get }
9}
10
11open class PNPresenceChannelGroupHereNowData : PNPresenceGlobalHereNowData {
12
13}
14
15open class PNPresenceChannelGroupHereNowResult : PNResult {
show all 19 linesOther examples
Returning state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
1/**
2 With .state client aside from occupancy and unique identifiers will will pull out
3 state information associated with them.
4 */
5self.client.hereNowForChannel("my_channel", withVerbosity: .state,
6 completion: { (result, status) in
7
8 if status == nil {
9
10 /**
11 Handle downloaded presence information using:
12 result.data.uuids - list of uuids. Each uuid entry will have next
13 fields: "uuid" - identifier and "state" if it
14 has been provided.
15 result.data.occupancy - total number of active subscribers.
show all 29 linesExample response
1{
2 "status" : 200,
3 "message" : "OK",
4 "service" : "Presence",
5 "uuids" : [
6 {
7 "uuid" : "myUUID0"
8 },
9 {
10 "state" : {
11 "abcd" : {
12 "age" : 15
13 }
14 },
15 "uuid" : "myUUID1"
show all 38 linesReturn occupancy only
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
You can return only the occupancy
information for a single channel by specifying the channel and setting UUIDs
to false:
1// With .occupancy client will pull out only occupancy information.
2self.client.hereNowForChannel("my_channel", withVerbosity: .occupancy,
3 completion: { (result, status) in
4
5 if status == nil {
6
7 /**
8 Handle downloaded presence information using:
9 result.data.occupancy - total number of active subscribers.
10 */
11 }
12 else {
13
14 /**
15 Handle presence audit error. Check 'category' property
show all 23 linesExample response
1{
2 "status": 200,
3 "message": "OK",
4 "payload": {
5 "channels": {
6 "81d8d989-b95f-443c-a726-04fac323b331": {
7 "uuids": [ "70fc1140-22b5-4abc-85b2-ff8c17b24d59" ],
8 "occupancy": 1
9 },
10 "81b7a746-d153-49e2-ab70-3037a75cec7e": {
11 "uuids": [ "91363e7f-584b-49cc-822c-52c2c01e5928" ],
12 "occupancy": 1
13 },
14 "c068d15e-772a-4bcd-aa27-f920b7a4eaa8": {
15 "uuids": [ "ccfac1dd-b3a5-4afd-9fd9-db11aeb65395" ],
show all 23 linesHere now for channel groups
Request information about subscribers on specific channel group. This API method will retrieve the list of UUIDs along with their state for each remote data object and the number of subscribers.