Implementing topics
Topics in Phoenix are a form of implementing the publisher-subscriber pattern.
In this recipe, we will create a simple counter in a form of a function that will subscribe a channel.
In TodosController
, we will broadcast an event every time a request is made to the index action. The counter will then receive the notification and will output a message to the console with the number of times the action was called.
Getting ready
To get started, we will take the code resulting from the previous recipe. Open a code editor and prepare to add pub/sub to the Phoenix application.
How to do it…
To implement topics in the Phoenix application, follow these steps:
Edit
TodoController
to make it look like this:defmodule Todo.TodosController do use Phoenix.Controller plug :action plug :render def index(conn, _params) do todos = [%{id: 1, task: "Write the other controller actions!", created_at: "2014-12-13", status: "pending"}, %{id: 2, task: "Create Views", created_at: "2014-12...