Fireship · @fireship
User Presence System in Realtime - Online, Offline, Away
Uploaded August 18, 2017 · Length 6:11
Summary
build a user presence system in real-time using angular and firebase to show online, offline, and away statuses
- angular
- firebase
- user presence system
- real-time status
- online offline away status
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
a real-time present system allows users
- 0:02
to know whether or not their fellow
- 0:04
users are online offline or away in some
- 0:07
other idle status in this episode we're
- 0:10
going to use slack as our inspiration to
- 0:12
build a system that will show the status
- 0:13
of users throughout the app we're
- 0:16
building a fully fledged system that
- 0:18
will be able to detect whether or not a
- 0:19
users engaging with an app based on a
- 0:21
timer and also whether or not a user has
- 0:24
closed the app and update the status on
- 0:26
each of these events at this point I'm
- 0:29
assuming you have a basic authentication
- 0:30
system in place with angularfire - if
- 0:32
not I've added a link in the description
- 0:34
that discusses that topic more
- 0:36
extensively the first thing we want to
- 0:38
do is see if the user has an active
- 0:40
connection to the firebase real time
- 0:42
database
- 0:43
firebase keeps track of this information
- 0:45
in a secret location called info /
- 0:47
connected which we can subscribe to just
- 0:50
like anything else in the database this
- 0:52
tells us whether the current client has
- 0:54
a connection to the database so let's go
- 0:56
into our off service and put this to use
- 0:58
first we'll import the angularfire
- 1:00
database as well as angularfire auth and
- 1:03
the main firebase SDK then we'll set a
- 1:06
variable for the current user is user ID
- 1:08
and inside the constructor will
- 1:11
subscribe to the angular fire off state
- 1:13
which is just the currently logged in
- 1:15
user and if we have a user we're going
- 1:18
to set their user ID on the service and
- 1:20
also subscribe to that connection data
- 1:23
that I just described earlier when we
- 1:25
actually determine the current user
- 1:26
status we'll save it as the status
- 1:28
property under their user ID and the
- 1:30
real time database we're going to update
- 1:32
the status frequently so we'll create a
- 1:34
helper function that we can just pass
- 1:36
the status as an argument and that will
- 1:38
update the database so any time we
- 1:40
detect a status change we can call this
- 1:42
helper and the database gets updated
- 1:44
from there we can create another
- 1:45
function that will subscribe to the info
- 1:48
/ connected data which will just be a
- 1:50
boolean true or false if it's true we'll
- 1:53
set the user status to online and it's
- 1:55
false will set it to offline then we
- 1:57
call our helper function and subscribe
- 1:59
and we're all set if we go to the app we


