Skip to content

State (Machine) of Enlightenment

Photo of Alex McLain
Hosted By
Alex M.
State (Machine) of Enlightenment

Details

Presenter: Ben Murphy

GenServer is the tried-and-true Elixir module for implementing a process that receives and handles messages. With a small number of mandatory callbacks that are able to encompass the majority of desired behavior for a server process, GenServers can be used both to quickly prototype code and to create robust processes that are central to any application. However, especially in Nerves-based applications, sometimes your code interacts with systems that behave unpredictably, erratically, or have quirks that require extra consideration to interact with. While the simplicity of a GenServer works for many developers in many cases, it can become a source of friction when trying to cover the edge cases that arise when interacting with such systems. A developer may feel like they're re-inventing the wheel, spending more time writing application logic than business logic to ensure their GenServer can handle everything.

The Erlang OTP offers a different module: :gen_statem. :gen_statem is similar to a GenServer in that it's made to manage a server process, but differs in that its implementation very closely models a state machine. GenServers store a blob of data conventionally referred to as a state, and developers may use information in this state blob as function guards or to otherwise affect control flow inside of their functions. By comparison, :gen_statem stores state information entirely separately from data information, allowing for conceptually cleaner transitions from one clearly defined state to another. The module also allows developers to return more complex directives to the underlying module code, silently implementing process behaviors that may take many lines of application code to implement with a GenServer. In this talk, we'll go over many of the features that :gen_statem offers out of the box that GenServer doesn't, and illustrate some real use cases that allow these features to shine. Once the full power of the state machine is understood both as a design pattern and as an OTP module, it will become a frequently-used tool to wrangle even the toughest system design problems.

Download the Zoom client or browser extension before the event at: https://zoom.us/download

The Nerves Project defines a new way to build embedded systems using Elixir.

Photo of Nerves group
Nerves
See more events
Online event
This event has passed