[erlang-questions] Is using gen_server behaviour appropriate for his usecase?

Vineet Naik naikvin@REDACTED
Fri Nov 30 10:49:27 CET 2012

Hi Roman,

Thanks for replying.

I think gen_server pretty much fits my case. But I would like to
know whether it's fine to use gen_server if the model is not
strictly based on client-server message passing. Does Erlang have
any thing like "abuse of the gen_server" anti-pattern :-) ?


On Fri, Nov 30, 2012 at 2:06 PM, Roman Gafiyatullin
<r.gafiyatullin@REDACTED> wrote:
> Hi,
> In most of the cases one shall not use "simple loop functions": raw process
> cannot participate properly in the supervision tree.
> If you need some new tricky specific model of behaving - better implement it
> over gen_server.
> For instance supervisor is a gen_server :)
> If you need something more low level - use 'gen' module (see how gen_fsm is
> implemented).
> --
> Regards
> RG ( +375 33 602 5080, UTC+3 )
> On Friday, November 30, 2012 at 11:25 am, Vineet Naik wrote:
> Hello,
> I am an Erlang newbie and trying to write a simple chat bot as an
> XMPP external component. I am using exmpp library and following
> along these tutorials[1]
> So far I have one module `bot_server` that uses the gen_server
> interface. Inside it's `handle_info` callback, incoming messages
> from various client will be received. To handle and reply to the
> these messages, I am thinking of spawning a "bot" process per
> client. It will stay alive as long as the client is available
> ie. when the client sends "unavailable" presence, it will die. I
> also need to keep a list of all the alive bot processes in the
> bot_server's state.
> My question is, would it be appropriate to implement the bot as a
> gen_server too considering that it needs to handle two calls, one
> for handling incoming message (asynchronous) and second for
> killing itself (synchronous)?
> In general, when should one use gen_server and when should
> one write a simple loop function?
> [1] exmpp tutorials:
> http://blog.process-one.net/scalable_xmpp_bots_with_erlang_and_exmpp_part_i/
> Thanks,
> Vineet
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

Vineet Naik

More information about the erlang-questions mailing list