Model state with GenServer: start, call and cast

11 steps · about 3 h 55 min · Intermediate

  1. Processes — Elixir v1.20.4 (doc, Elixir official docs, 20 min)

    A GenServer is a process, so first learn spawn, send, receive and a looping process that keeps state.

  2. Elixir, A Little Beyond The Basics - Part 6: processes (tutorial, openmymind.net, 20 min)

    A second explanation of mailboxes and receive loops, with a hand-built calculator process.

  3. Processes in Elixir on Exercism (interactive, Exercism, 30 min)

    Exercism gives a short reference on the receive loop that carries state, which is the pattern GenServer standardises.

  4. Agent — Elixir v1.12.3 (doc, Elixir official docs, 15 min)

    Agents are the simplest wrapper around state, so they make a good contrast before moving to GenServer.

  5. Intro to GenServer (video, ElixirCasts, 10 min)

    A short first look at a GenServer in action before you read the formal docs.

  6. Client-server with GenServer — Elixir v1.20.5 (guide, Elixir official Getting Started guide, 30 min)

    The official walkthrough rewrites an Agent as a GenServer and covers the callbacks and start_link.

  7. GenServer — Elixir v1.20.5 (doc, Elixir official docs, 40 min)

    The reference for init/1, handle_call/3, handle_cast/2, name registration and when not to use a GenServer.

  8. GenServer (article, Elixir Examples, 15 min)

    Counter and key-value examples split the client API from the server callbacks and give a rule for choosing call or cast.

  9. How to write an Elixir Gen Server (video, YouTube, 20 min)

    Watch someone write call and cast handlers live to check your own implementation.

  10. Deconstructing Elixir's GenServers (blog, AppSignal, 20 min)

    Rebuilding call and cast yourself shows what GenServer does underneath.

  11. Elixir GenServer: Concurrent Stateful Process Implementation (article, Curiosum, 15 min)

    A game-state example you can run in iex that also introduces handle_info for plain messages.