Retrieving Presence data in ETS

The final piece of our puzzle is simple. We're going to do this purely as an exercise in understanding how the data is recorded in ETS. In reality, we'd want to lock this information down a little bit more to something like admins-only access. That's okay, though—the point of this is to learn and get a great handle on how to use this information. To make our lives easier, we'll implement a history action on our page controller and just build a simple list of all of the actions in the history logs:

  1. We'll start by adding a new entry into our routes file at lib/vocial_web/router.ex. In our main scope, add the following line:
get "/history", PageController, :history
  1. In PageController (lib/vocial_web/controllers/page_controller.ex), we'll alias our ChatCache module and add the history function:
alias Vocial.ChatCache
  def history(conn, _params) do
render conn, "history.html", logs: ChatCache.lookup()
end
  1. For the final piece of this implementation puzzle, we'll create a new template under lib/vocial_web/templates/page/history.html.eex:
<div class="container">
<h2>Chat History</h2>
<ul class="list-unstyled">
<%= for log <- @logs do %>
<li>
<em>#<%= log.topic %></em>
&lt;<%= log.username %>&gt;
<strong><%= log.status %></strong>
at <%= log.timestamp %>
</li>
<% end %>
</ul>
</div>

And that's it! If we head on over to our chat application, we can do a bunch of joins and idles and then open up our Chat History page, located at /history. We should see something like the following representing the user's status changing from active to idle periodically:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset