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
Copy file name to clipboardExpand all lines: README.md
+42-2Lines changed: 42 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -581,11 +581,51 @@ Firestack attempts to provide the same API as the JS Firebase library for both A
581
581
582
582
// TODO: Finish documenting
583
583
584
-
#### onDisconnect()
584
+
###Presence
585
585
586
-
Firebase provides a nice platform for defining presence. Firestack allows us to handle this as well using the `onDisconnect()` event.
586
+
Firestack comes in with a built-in method for handling user connections. We just need to set the presence ref url and tell Firestack to keep track of the user by their child path.
587
587
588
+
```javascript
589
+
firestack.presence// the presence api
590
+
.on('users/connections') // set the users/connections as the
591
+
// root for presence handling
592
+
.setOnlineFor('auser') // Set the child of auser as online
593
+
```
594
+
595
+
While the _device_ is online (the connection), the value of the child object at `users/connections/auser` will be:
596
+
597
+
```javascript
598
+
{
599
+
online:true,
600
+
lastOnline:TIMESTAMP
601
+
}
602
+
```
603
+
604
+
When the device is offline, the value will be updated with `online: false`:
588
605
606
+
```javascript
607
+
{
608
+
online:false,
609
+
lastOnline:TIMESTAMP
610
+
}
611
+
```
612
+
613
+
To set up your own handlers on the presence object, you can call `onConnect()` and pass a callback. The method will be called with the `connectedDevice` database reference and you can set up your own handlers:
614
+
615
+
```javascript
616
+
constpresence=firestack.presence
617
+
.on('users/connections');
618
+
presence.onConnect((ref) => {
619
+
ref.onDisconnect().remove(); // Remove the entry
620
+
// or
621
+
ref.set({
622
+
location: someLocation
623
+
});
624
+
// or whatever you want as it's called with the database
625
+
// reference. All methods on the DatabaseRef object are
0 commit comments