Monday, October 20, 2014

Ejabberd plugin

Ejabberd is very popular scalable XMPP server. This server is an instant messaging or chatting server. It is written in erlang. It implements it’s features in forms of modules. These modules can be seen as plugins. Here is how it works. This is our continuous effort to investigate plugins in different projects.

Here the mod_last is taken as example. Most of the modules here in ejabberd are small and less than 500 lines. Here is a small window of the output of find -iname "mod_*.erl" | xargs wc in linux shell. The values in left column are the number of lines.

    198     825    8244 ./src/mod_echo.erl
    173     566    5827 ./src/mod_proxy65_sm.erl
    682    1991   22312 ./src/mod_register.erl
    351    1082   11792 ./src/mod_last.erl
   1463    4336   48959 ./src/mod_shared_roster.erl
   1347    3897   45857 ./src/mod_irc.erl
    270    1087   10304 ./src/mod_carboncopy.erl
    109     380    3650 ./src/mod_time.erl
     97     330    3437 ./src/mod_service_log.erl

It is interesting that mod_last contains only 351 lines of code including the code comments. So this file will be easy to follow.

The purpose of this module

This module keeps a record of the time when a person is last seen online.

Loading and unloading

The modules here are loadable. They have a start and stop functions to initiate and deinitialize them. The start function takes two arguments and stop takes one argument.

Extension points

The start function here registers hook for following events.

  • unset_presence_hook
    The on_presence_update function is called when user is logged out of server. It just stores the time entry in the database.
    ejabberd_hooks:add(unset_presence_hook, Host, ?MODULE,
                       on_presence_update, 50).
on_presence_update(User, Server, _Resource, Status) ->
    TimeStamp = now_to_seconds(now()),
    store_last_info(User, Server, TimeStamp, Status).
  • remove_user
    When the user is removed permanently from server he is deleted from last_activity database.
    ejabberd_hooks:add(remove_user, Host, ?MODULE,
                       remove_user, 50),

There are other hooks that answer the user activity information and do other things.


This is very interesting to see how small the code is. And it is very precise too. Programmers will love to write modules here. It can be given 500 points for the mechanism (there will be more points coming for other projects). Thanks to erlang, nice language.

No comments: