Gen(eric) behaviours
OTP defines several generic behaviours we can use when creating Elixir applications. There is the GenServer
behaviour, the GenEvent
behaviour, and the :gen_fsm
behaviour. All of these behaviours have their foundation in an even more general behaviour of OTP processes.
These behaviours remove some of the tedious work we had to do for handling messages and performing work that we encountered in the previous chapter.
We will start with our discussion on GenServer
, and then move onto more specialized variants.
Gen(eric) servers
OTP gives us the basic blueprint for a process that receives messages, processes messages and sends a result back, like any server would.
Gen in GenServer
really stands for generic or general because it provides the general details of such a process without constraining its users too much into an inflexible solution. For example, we saw that the main event loop of the processes we wrote in the previous chapter were all very similar in nature; the only...