Skip to content

Commit 778a005

Browse files
committed
Updated presence with README
1 parent 38a2287 commit 778a005

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,51 @@ Firestack attempts to provide the same API as the JS Firebase library for both A
581581

582582
// TODO: Finish documenting
583583

584-
#### onDisconnect()
584+
### Presence
585585

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.
587587

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`:
588605

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+
const presence = 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
626+
// available here on the `ref`
627+
})
628+
```
589629

590630
### ServerValue
591631

lib/modules/presence.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class PresenceRef extends ReferenceBase {
4343
})
4444
}
4545

46+
onConnect(cb) {
47+
this._onConnect.push(cb);
48+
}
49+
4650
}
4751

4852
export class Presence extends Base {

0 commit comments

Comments
 (0)