View Source GenEvent behaviour (Elixir v1.10.4)

This behaviour is deprecated. Use Erlang/OTP's :gen_event module instead.

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.

Link to this section Summary

Link to this section Types

@type handler() :: atom() | {atom(), term()}
@type manager() :: pid() | name() | {atom(), node()}
@type name() :: atom() | {:global, term()} | {:via, module(), term()}
@type on_start() :: {:ok, pid()} | {:error, {:already_started, pid()}}
@type options() :: [{:name, name()}]

Link to this section Callbacks

Link to this callback

code_change(old_vsn, state, extra)

View Source
@callback code_change(old_vsn, state :: term(), extra :: term()) ::
  {:ok, new_state :: term()}
when old_vsn: term() | {:down, term()}
Link to this callback

handle_call(request, state)

View Source
@callback handle_call(request :: term(), state :: term()) ::
  {:ok, reply, new_state}
  | {:ok, reply, new_state, :hibernate}
  | {:remove_handler, reply}
when reply: term(), new_state: term()
Link to this callback

handle_event(event, state)

View Source
@callback handle_event(event :: term(), state :: term()) ::
  {:ok, new_state} | {:ok, new_state, :hibernate} | :remove_handler
when new_state: term()
@callback handle_info(msg :: term(), state :: term()) ::
  {:ok, new_state} | {:ok, new_state, :hibernate} | :remove_handler
when new_state: term()
@callback init(args :: term()) ::
  {:ok, state} | {:ok, state, :hibernate} | {:error, reason :: any()}
when state: any()
Link to this callback

terminate(reason, state)

View Source
@callback terminate(reason, state :: term()) :: term()
when reason:
       :stop | {:stop, term()} | :remove_handler | {:error, term()} | term()