O'Reilly logo

Elixir Cookbook by Paulo A Pereira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

  1. Edit TodoController to make it look like this:
    defmodule Todo.TodosController do use ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required