Erlang : Writing First program Erlang for Ejabberd with example

Ejabberd is the XMPP server developed in Erlang language widely used for chatting purpose. Below is the set of Hints to write your custom code Erlang for Ejabberd server.

Hints for Erlang Program

  • Erlang supports primitive data type to create variables.

  • The variable names are starts with Capital letters [Ex: Arg, Resource, Server]

  • Erlang codes are saved with extension of "erl".

  • In export we will declare functions with arguments for access.

Ex: -export([helloworld/2]).
  • Each functions declarations are written in same file, declaration part start with ->. Each lines are ended with Semicolon(,) and End of function is defined with Dot (.)

  • The optional parameters of function are starts with underscore(_).

Ex:

start(_Host, _Opts) ->
   INFO_MSG("CodeExpertz is best Tutorial site");
   ok.
  • Module names are defined in top of the Erlang file “-module(MODULE_NAME). Ex: -module(mod_first_program).

  • Adding Supporting Files using the function -include(LIBRARY_FILENAME).

Ex: -include("ejabberd.hrl").

Example:

-module(mod_hello).
 
-behaviour(gen_mod).
 
-include("logger.hrl").
 
%% gen_mod API callbacks
-export([start/2, stop/1]).
 
start(_Host, _Opts) ->
    ok = case inets:start() of
      {error, {already_started, inets}} ->
          ok;
      ok ->
          ok
    end,
    ejabberd_hooks:add(user_send_packet, Host,
                       ?MODULE, log_user_send, 50),
    httpc:request(post,{"http://192.168.99.69:8083/api/data", [], "application/x-www-form-urlencoded","example=here&foo=bar"}, [], []).
    ok.
 
stop(_Host) ->
    httpc:request(post,{"http://192.168.99.69:8083/api/data", [], "application/x-www-form-urlencoded","example=here&foo=bar"}, [], []).
    ok.

 

Category: