GenEvent behaviour

WARNING: this module is deprecated.

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.

Summary

Types

handler()
manager()
name()
on_start()
options()

Callbacks

code_change(old_vsn, state, extra)
handle_call(request, state)
handle_event(event, state)
handle_info(msg, state)
init(args)
terminate(reason, state)

Types

handler()

handler() :: atom() | {atom(), term()}

manager()

manager() :: pid() | name() | {atom(), node()}

name()

name() :: atom() | {:global, term()} | {:via, module(), term()}

on_start()

on_start() :: {:ok, pid()} | {:error, {:already_started, pid()}}

options()

options() :: [{:name, name()}]

Callbacks

code_change(old_vsn, state, extra)

code_change(old_vsn, state :: term(), extra :: term()) ::
  {:ok, new_state :: term()}
when old_vsn: term() | {:down, term()}

handle_call(request, state)

handle_call(request :: term(), state :: term()) ::
  {:ok, reply, new_state}
  | {:ok, reply, new_state, :hibernate}
  | {:remove_handler, reply}
when reply: term(), new_state: term()

handle_event(event, state)

handle_event(event :: term(), state :: term()) ::
  {:ok, new_state} | {:ok, new_state, :hibernate} | :remove_handler
when new_state: term()

handle_info(msg, state)

handle_info(msg :: term(), state :: term()) ::
  {:ok, new_state} | {:ok, new_state, :hibernate} | :remove_handler
when new_state: term()

init(args)

init(args :: term()) ::
  {:ok, state} | {:ok, state, :hibernate} | {:error, reason :: any()}
when state: any()

terminate(reason, state)

terminate(reason, state :: term()) :: term()
when reason:
       :stop | {:stop, term()} | :remove_handler | {:error, term()} | term()

© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/elixir/1.6.6/GenEvent.html