Fireship · @fireship
Realtime User Presence with Firebase & Angular - Online | Offline | Away
Uploaded January 28, 2019 · Length 9:22
Summary
Implementing real-time user presence with Firebase and Angular, handling online, offline, and away states.
- firebase presence
- angular presence
- realtime user presence
- online offline away status
- firebase real-time database
- angularfire
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
[Music]
- 0:06
if you're building a social media or
- 0:08
chat app having real-time user presence
- 0:10
is very likely a critical feature
- 0:12
fortunately the firebase real-time
- 0:14
database makes it very easy to determine
- 0:16
whether or not a client is connected to
- 0:17
the database in today's video we'll use
- 0:19
this functionality to build a present
- 0:21
system that will show whether a user is
- 0:23
online offline or way if you're new to
- 0:25
the channel I can subscribe and make
- 0:27
sure to follow along with the full
- 0:28
source code on fire ship IO the first
- 0:30
thing that I want to point out is that
- 0:31
there are a lot of different edge cases
- 0:33
when building a presence system in
- 0:34
today's video we'll look at five
- 0:35
different situations that the user can
- 0:37
be in if they're signing and using the
- 0:39
app they should be online if they're
- 0:40
signed in but the app is closed they'll
- 0:42
be offline but if they're only on a
- 0:43
different browser tab then we'll just
- 0:45
set the status to away then in scenario
- 0:47
number four the user might be signed out
- 0:48
but still have the app open in which
- 0:50
case the connection will still be active
- 0:51
because firebase tracks the connection
- 0:53
to the device and not the authentication
- 0:55
state and if they're signed out in the
- 0:57
app is closed and of course the status
- 0:58
should be offline now let's take a look
- 1:00
at the database model that we'll use for
- 1:02
this we're in the real time database and
- 1:04
we have a note called status then for
- 1:06
each user will create a node based on
- 1:07
their user ID which contains the status
- 1:09
itself and also a timestamp for when it
- 1:11
was changed now let's look at how we
- 1:13
consume this data from the front-end
- 1:14
application on the Left we have the
- 1:16
actual user and then on the right we
- 1:17
just have an incognito browser to
- 1:19
observe the changes in the state when we
- 1:21
log in on the left you'll see that the
- 1:22
browser on the right also gets an
- 1:24
updated status for the user if these are
- 1:27
on the Left then switches to a different
- 1:28
browser tab the status will update to
- 1:30
away I just had to check my myspace for
- 1:32
a minute but when I flip back it will
- 1:34
immediately go back to online now if I
- 1:36
close the browser tab on the left it
- 1:38
will set my status to offline even
- 1:39
though I didn't technically sign out so
- 1:41
the general idea here is that we're
- 1:43
listening to state changes to the users
- 1:45
connection to the database as well as
- 1:46
their authentication state and then
- 1:48
updating the database in response to
- 1:49
those events and you may have noticed
- 1:51
that we're using the real-time database
- 1:53
and not firestorm that's because
- 1:54
presence is not currently supported in
- 1:56
firestore hopefully it is in the future
- 1:57
but for right now you can set up a cloud
- 1:59
function to mirror this data in fire


