Fireship · @fireship

Realtime User Presence with Firebase & Angular - Online | Offline | Away

Uploaded January 28, 2019 · Length 9:22

Realtime User Presence with Firebase & Angular - Online | Offline | Away

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.

  1. 0:00

    [Music]

  2. 0:06

    if you're building a social media or

  3. 0:08

    chat app having real-time user presence

  4. 0:10

    is very likely a critical feature

  5. 0:12

    fortunately the firebase real-time

  6. 0:14

    database makes it very easy to determine

  7. 0:16

    whether or not a client is connected to

  8. 0:17

    the database in today's video we'll use

  9. 0:19

    this functionality to build a present

  10. 0:21

    system that will show whether a user is

  11. 0:23

    online offline or way if you're new to

  12. 0:25

    the channel I can subscribe and make

  13. 0:27

    sure to follow along with the full

  14. 0:28

    source code on fire ship IO the first

  15. 0:30

    thing that I want to point out is that

  16. 0:31

    there are a lot of different edge cases

  17. 0:33

    when building a presence system in

  18. 0:34

    today's video we'll look at five

  19. 0:35

    different situations that the user can

  20. 0:37

    be in if they're signing and using the

  21. 0:39

    app they should be online if they're

  22. 0:40

    signed in but the app is closed they'll

  23. 0:42

    be offline but if they're only on a

  24. 0:43

    different browser tab then we'll just

  25. 0:45

    set the status to away then in scenario

  26. 0:47

    number four the user might be signed out

  27. 0:48

    but still have the app open in which

  28. 0:50

    case the connection will still be active

  29. 0:51

    because firebase tracks the connection

  30. 0:53

    to the device and not the authentication

  31. 0:55

    state and if they're signed out in the

  32. 0:57

    app is closed and of course the status

  33. 0:58

    should be offline now let's take a look

  34. 1:00

    at the database model that we'll use for

  35. 1:02

    this we're in the real time database and

  36. 1:04

    we have a note called status then for

  37. 1:06

    each user will create a node based on

  38. 1:07

    their user ID which contains the status

  39. 1:09

    itself and also a timestamp for when it

  40. 1:11

    was changed now let's look at how we

  41. 1:13

    consume this data from the front-end

  42. 1:14

    application on the Left we have the

  43. 1:16

    actual user and then on the right we

  44. 1:17

    just have an incognito browser to

  45. 1:19

    observe the changes in the state when we

  46. 1:21

    log in on the left you'll see that the

  47. 1:22

    browser on the right also gets an

  48. 1:24

    updated status for the user if these are

  49. 1:27

    on the Left then switches to a different

  50. 1:28

    browser tab the status will update to

  51. 1:30

    away I just had to check my myspace for

  52. 1:32

    a minute but when I flip back it will

  53. 1:34

    immediately go back to online now if I

  54. 1:36

    close the browser tab on the left it

  55. 1:38

    will set my status to offline even

  56. 1:39

    though I didn't technically sign out so

  57. 1:41

    the general idea here is that we're

  58. 1:43

    listening to state changes to the users

  59. 1:45

    connection to the database as well as

  60. 1:46

    their authentication state and then

  61. 1:48

    updating the database in response to

  62. 1:49

    those events and you may have noticed

  63. 1:51

    that we're using the real-time database

  64. 1:53

    and not firestorm that's because

  65. 1:54

    presence is not currently supported in

  66. 1:56

    firestore hopefully it is in the future

  67. 1:57

    but for right now you can set up a cloud

  68. 1:59

    function to mirror this data in fire