You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Presence channels are created in exactly the same way as private channels, except that they reside in the 'presence-' namespace.
470
+
Presence channels are channels whose names are prefixed by `presence-`.
471
+
472
+
The recommended way of subscribing to a presence channel is to use the `subscribeToPresenceChannel` function, as opposed to the standard `subscribe` function. Using the `subscribeToPresenceChannel` function means that you get a `PusherPresenceChannel` object returned, as opposed to a standard `PusherChannel`. This `PusherPresenceChannel` object has some extra, presence-channel-specific functions availalbe to it, such as `members`, `me`, and `findMember`.
471
473
472
474
#### Swift
473
475
```swift
474
-
let myPresenceChannel = pusher.subscribe("presence-my-channel")
476
+
let myPresenceChannel = pusher.subscribeToPresenceChannel(channelName: "presence-my-channel")
This will give you back a `PusherChannel` object, which will not have access to functions available to `PusherPresenceChannel` objects, such as `members`, `me` etc.
483
-
484
-
You can of course cast the `PusherChannel` object to a `PusherPresenceChannel` or you can instead use the `subscribeToPresenceChannel` function which will directly return a `PusherPresenceChannel` object. You can do so like this:
484
+
As alluded to, you can still subscribe to presence channels using the `subscribe` method, but the channel object you get back won't have access to the presence-channel-specific functions, unless you choose to cast the channel object to a `PusherPresenceChannel`.
485
485
486
486
#### Swift
487
487
```swift
488
-
let myPresenceChannel = pusher.subscribeToPresenceChannel(channelName: "presence-my-channel")
488
+
let myPresenceChannel = pusher.subscribe("presence-my-channel")
You can also provide functions that will be called when members are either added to or removed from the channel. These are available as parameters to both `subscribe` and `subscribeToPresenceChannel`.
@@ -500,13 +500,98 @@ You can also provide functions that will be called when members are either added
500
500
let onMemberChange = { (member: PusherPresenceChannelMember) in
501
501
print(member)
502
502
}
503
-
let chan = pusher.subscribe("presence-channel", onMemberAdded: onMemberChange, onMemberRemoved: onMemberChange)
503
+
504
+
let chan = pusher.subscribeToPresenceChannel("presence-channel", onMemberAdded: onMemberChange, onMemberRemoved: onMemberChange)
**Note**: The `members` and `myId` properties of `PusherPresenceChannel` objects (and functions that get the value of these properties) will only be set once subscription to the channel has succeeded.
517
+
518
+
The easiest way to find out when a channel has been successfully susbcribed to is to bind to the event named `pusher:subscription_succeeded` on the channel you're interested in. It would look something like this:
519
+
520
+
#### Swift
521
+
```swift
522
+
let pusher = Pusher(key: "YOUR_APP_KEY")
523
+
524
+
let chan = pusher.subscribeToPresenceChannel("presence-channel")
525
+
526
+
let _ = chan.bind(eventName: "pusher:subscription_succeeded", callback: { data in
527
+
print("Subscribed!")
528
+
print("I can now access myId: \(chan.myId)")
529
+
print("And here are the channel members: \(chan.members)")
Note that both private and presence channels require the user to be authenticated in order to subscribe to the channel. This authentication can either happen inside the library, if you configured your Pusher object with your app's secret, or an authentication request is made to an authentication endpoint that you provide, again when instantiaing your Pusher object.
507
591
508
592
We recommend that you use an authentication endpoint over including your app's secret in your app in the vast majority of use cases. If you are completely certain that there's no risk to you including your app's secret in your app, for example if your app is just for internal use at your company, then it can make things easier than setting up an authentication endpoint.
509
593
594
+
510
595
## Binding to events
511
596
512
597
Events can be bound to at 2 levels; globally and per channel. When binding to an event you can choose to save the return value, which is a unique identifier for the event handler that gets created. The only reason to save this is if you're going to want to unbind from the event at a later point in time. There is an example of this below.
0 commit comments