The gen_server Callback Structure

Now that we understand the idea, we’ll take a more detailed look at the gen_server callback structure.

Starting the Server

The call gen_server:start_link(Name, Mod, InitArgs, Opts) starts everything. It creates a generic server called Name. The callback module is Mod. Opts controls the behavior of the generic server. Here we can specify the logging of messages, debugging of functions, and so on. The generic server starts by calling Mod:init(InitArgs).

The template entry for init is shown in Figure 2, init template entry (the full template can be found in The Generic Server Template):

 
%%--------------------------------------------------------------------
 
%% @private
 
%% @doc
 
%% Initializes the ...

Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.