Using GenServers

Let's go back and look at the GenServer we implemented for the previous chapter:

defmodule Vocial.ChatCache do  use GenServer

We started off by declaring that we wanted to use the GenServer macros as a starting point for building off our ChatCache Genserver. GenServer.start_link/0 should take in the module that is running as the server, the arguments to starting the link, and the list of options for starting the server itself. In the following case, we're going to register this running process as a name, using its module name as the name:

  def start_link do    GenServer.start_link(__MODULE__, [], name: __MODULE__)  end

Next, we need to declare an entry point for our GenServer. This is how our Supervisor knows how to start up our ...

Get Phoenix Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.