Fireship · @fireship

User Presence System in Realtime - Online, Offline, Away

Uploaded August 18, 2017 · Length 6:11

User Presence System in Realtime - Online, Offline, Away

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.

  1. 0:00

    a real-time present system allows users

  2. 0:02

    to know whether or not their fellow

  3. 0:04

    users are online offline or away in some

  4. 0:07

    other idle status in this episode we're

  5. 0:10

    going to use slack as our inspiration to

  6. 0:12

    build a system that will show the status

  7. 0:13

    of users throughout the app we're

  8. 0:16

    building a fully fledged system that

  9. 0:18

    will be able to detect whether or not a

  10. 0:19

    users engaging with an app based on a

  11. 0:21

    timer and also whether or not a user has

  12. 0:24

    closed the app and update the status on

  13. 0:26

    each of these events at this point I'm

  14. 0:29

    assuming you have a basic authentication

  15. 0:30

    system in place with angularfire - if

  16. 0:32

    not I've added a link in the description

  17. 0:34

    that discusses that topic more

  18. 0:36

    extensively the first thing we want to

  19. 0:38

    do is see if the user has an active

  20. 0:40

    connection to the firebase real time

  21. 0:42

    database

  22. 0:43

    firebase keeps track of this information

  23. 0:45

    in a secret location called info /

  24. 0:47

    connected which we can subscribe to just

  25. 0:50

    like anything else in the database this

  26. 0:52

    tells us whether the current client has

  27. 0:54

    a connection to the database so let's go

  28. 0:56

    into our off service and put this to use

  29. 0:58

    first we'll import the angularfire

  30. 1:00

    database as well as angularfire auth and

  31. 1:03

    the main firebase SDK then we'll set a

  32. 1:06

    variable for the current user is user ID

  33. 1:08

    and inside the constructor will

  34. 1:11

    subscribe to the angular fire off state

  35. 1:13

    which is just the currently logged in

  36. 1:15

    user and if we have a user we're going

  37. 1:18

    to set their user ID on the service and

  38. 1:20

    also subscribe to that connection data

  39. 1:23

    that I just described earlier when we

  40. 1:25

    actually determine the current user

  41. 1:26

    status we'll save it as the status

  42. 1:28

    property under their user ID and the

  43. 1:30

    real time database we're going to update

  44. 1:32

    the status frequently so we'll create a

  45. 1:34

    helper function that we can just pass

  46. 1:36

    the status as an argument and that will

  47. 1:38

    update the database so any time we

  48. 1:40

    detect a status change we can call this

  49. 1:42

    helper and the database gets updated

  50. 1:44

    from there we can create another

  51. 1:45

    function that will subscribe to the info

  52. 1:48

    / connected data which will just be a

  53. 1:50

    boolean true or false if it's true we'll

  54. 1:53

    set the user status to online and it's

  55. 1:55

    false will set it to offline then we

  56. 1:57

    call our helper function and subscribe

  57. 1:59

    and we're all set if we go to the app we