Elixir foundationSort Order

Elixir: GenServer Init - [013]

We'll take a very brief look at the simplest GenServer. You can even regard this as the "hello world" of GenServers.

GenServer Init

defmodule Botiful.Render.Engine do  use GenServer    def start_link(url) do    GenServer.start_link(__MODULE__, url)  end    def init(url) do    {:ok, %{url: url}}  endend

Let's dissect this. We split our code into 2 sections, the public api which is the start_link function this is the function we will use to start the GenServer . The next function we defined is the init function, this is the 'callback' section of the module. We don't actually call the init function as the user of this module but our code will call this function.

Starting our GenServer

Let's start our GenServer. Go ahead and boot the app up using iex -S mix

{:ok, pid} = Botiful.Render.Engine.start_link(url)

That's it! Our GenServer has started! It doesn't do anything but it's definitely started and we can access it using the pid

We'll explore GenServer more in the next episode.