Use Tracker in an Application

Learn how to use Phoenix Tracker in an application.

Tracker in HelloSockets

The HelloSocket application from earlier chapters serves as a reference project that provides examples of using different Phoenix features. We’ll add our Phoenix.Tracker code to this application so that we have an example to revisit if needed. We’ll continue with our Sneakers23 application shortly.

If we have completed the earlier sections of this course, our hello_sockets project will serve as the starting code for this Tracker project. Follow the instructions in the “Set Up Your Project” section from earlier in this chapter.

It’s easy to get started with Tracker in an application. We will first define a module that implements the Phoenix.Tracker behavior. This module hides the Tracker function calls and provides a simple interface for our Channels to use. We’ll then track our desired Channel processes when they join. Finally, we’ll use the Phoenix.Tracker.list/2 function to get all the tracked data.

For this example, we’ll revisit the HelloSockets application. First, create a new module at the path lib/hello_sockets_web/channels/user_tracker.ex.

Get hands-on with 1200+ tech skills courses.