View Source GenEvent behaviour (Elixir v1.12.3)
A event manager with event handlers behaviour.
If you are interested in implementing an event manager, please read the
"Alternatives" section below. If you have to implement an event handler to
integrate with an existing system, such as Elixir's Logger, please use
:gen_event
instead.
Alternatives
There are a few suitable alternatives to replace GenEvent. Each of them can be the most beneficial based on the use case.
Supervisor and GenServers
One alternative to GenEvent is a very minimal solution consisting of using a supervisor and multiple GenServers started under it. The supervisor acts as the "event manager" and the children GenServers act as the "event handlers". This approach has some shortcomings (it provides no backpressure for example) but can still replace GenEvent for low-profile usages of it. This blog post by José Valim has more detailed information on this approach.
GenStage
If the use case where you were using GenEvent requires more complex logic, GenStage provides a great alternative. GenStage is an external Elixir library maintained by the Elixir team; it provides a tool to implement systems that exchange events in a demand-driven way with built-in support for backpressure. See the GenStage documentation for more information.
:gen_event
If your use case requires exactly what GenEvent provided, or you have to
integrate with an existing :gen_event
-based system, you can still use the
:gen_event
Erlang module.