Agent

As we saw in the previous section, GenServer lets you create a server process that can handle synchronous and asynchronous interactions with its clients, while also giving you a way to handle other kinds of low-level messages (sent using Process.send/3) with the handle_info/2 callback.

However, there will be times when you don't want to control whether the communication is sync or asynchronous. You don't need to handle out-of-band messages, you just want to spawn a process that properly stores information in its internal state and shares it when needed. In this case, a GenServer feels overkill for the task at hand.

Instead, you can employ an Agent and get it over with. Think of it as a GenServer tailored to only perform internal state ...

Get Mastering Elixir 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.