Home > English, Erlang > Connecting an XMPP external component via Erlang and EXMPP

Connecting an XMPP external component via Erlang and EXMPP

Here I am again. Sorry for the time it took me to write a new blog entry, but I was preparing the speech for my bachelor thesis..so as you could understand that took all of my free time.
My thesis explained how social networking could be integrated in an XMPP environment, and I have to say that during the time it took me to design and write the code I became an XMPP nuts. XMPP is a protocol aimed to real-time messaging and presence, but it allows developers to use many extensions to implement different kind of services.
For my thesis I used as XMPP server the software Tigase and since it is developed in Java, I wrote all of my code in the same language, but I was aware of the fact that a XMPP server developed in Erlang and called eJabberd exists so I decided to port all of my code in Erlang.
In this post I will teach you how enable an external component in eJabberd and how to code it using Erlang and EXMPP a library provided by Process One. First of all we have to enable the external component in the eJabberd configuration file; by default you can find it in /etc/ejabberd, so:
bellerofonte@pegaso:~$ cd /etc/ejabberd/
belleforonte@pegaso:/etc/ejabberd$ ls
ejabberd.cfg  ejabberdctl.cfg  inetrc

At this point let’s edit ejabberd.cfg (you may be asked to do it with the sudo option).

bellerofonte@pegaso:~$ sudo nano ejabberd.cfg

Now you have to find the part of the configuration file that starts with:

%% ejabberd_service: Interact with external components (transports...)

And add the following lines to add two different external components:

  {8888, ejabberd_service, [
                          {access, all},
                          {shaper_rule, fast},
                          {ip, {127, 0, 0, 1}},
                          {hosts, ["weather.localhost", "weather.trentino.com"],
                           [{password, "weather"}]

  {8888, ejabberd_service, [
                          {access, all},
                          {shaper_rule, fast},
                          {ip, {127, 0, 0, 1}},
                          {hosts, ["news.localhost", "news.trentino.com"],
                           [{password, "news"}]

If you don’t know about XMPP and external component I can just tell you that the names I specified in the list after hosts represent the names that will be used to identify this component in the XMPP network, while the value of password is just the password used by the component to perform the handshake with the server. For more info you can read this.

At this point save the configuration file and restart eJabberd.

Now let’s use EXMPP to connect from the Erlang shell to eJabberd as if we were one of the two components.

bellerofonte@pegaso:/etc/ejabberd$ erl
Erlang R13B03 (erts-5.7.4) 

Eshell V5.7.4  (abort with ^G)
1> exmpp:start().
2> Session = exmpp_component:start_link().
3> exmpp_component:auth(Session, "weather", "weather").
4> _StreamId = exmpp_component:connect(Session, "localhost", 8888).
5>  ok = exmpp_component:handshake(Session).

In the code above, I first started  the exmpp application, then I created a Session for the XMPP component and made the authorization stream by passing to the function exmpp_component the session, the name used by the component and the password needed  for the handshake.

Then I connected it to the eJabberd server by using the function connect, which needs the session, the name of the server and the port on which it is listening to.

After that just connect the component by using the function handshake.

Now, you can log in to the server with one test account of yours and perform a service discovery, the result should be something like:

PSI: Service Discovery

Ok, I know this post was kinda short, but I promise that next one will be much better, and in it I will teach you how to receive and send XMPP stanzas from our component!

Categories: English, Erlang Tags: , ,
  1. NV
    April 5, 2010 at 7:41 pm

    Couldn’t help but notice that neither component is in that final screenshot?

    • pdincau
      April 5, 2010 at 8:05 pm


      actually you are right…but meteo is the italian word for weather…i embedded the wrong image in the post…anyhow that one is a bot loaded in the same way showed in the tutorial above.



  2. June 19, 2010 at 2:45 pm

    Hi, I have tried using exmpp to talk to gtalk, can you give a sample short code to connect to gtalk. ?

    • pdincau
      June 20, 2010 at 7:12 pm

      Do you mean a client connection to gmail? I will take a look at it…

    • pdincau
      June 21, 2010 at 7:17 am


      it seems that SASL is fully supported in exmpp but not TLS…i’m not sure you can connect to gtalk only using SASL.

      • pdincau
        June 22, 2010 at 10:08 am

        I asked mickael from process one and connection to gmail is not possible usgin exmpp..we should wait new releases!

  3. Revence
    August 5, 2010 at 1:32 pm

    Ah, I see. So we don’t have SSL in EXMPP, and that would explain why we cannot use it to connect to Google Talk.
    Hmm. I wonder if it would too difficult to add the code that upgrades the socket … Let me try it.

  4. ppolv
    August 7, 2010 at 9:48 pm

    Hello, just for the record, TLS support was added in recent versions of exmpp (not sure if it is included in latest package, but sure it is available on GIT).
    So now it is possible to connect to gtalk, the procedure is:

    PASS = “YourPassword”,
    StringJID = “YourUsername@gmail.com/test”,
    Ses = exmpp_session:start({1,0}),
    Jid = exmpp_jid:parse(StringJID),
    exmpp_session:auth_info(Ses, Jid, PASS),
    {ok, _StreamId, _Features} = exmpp_session:connect_TCP(Ses, “talk.l.google.com”, 5222,[{starttls, enabled}]),
    {ok, _FinalJID} = exmpp_session:login(Ses, “PLAIN”)

    Note that the login procedure is still cumbersome, expect some API changes on it in the future.
    Instead of directly including the gmail server’s hostnames (“talk.l.google.com”) you should use exmpp_dns:get_c2s(“gmail.com”) to query for them. (currently exmpp don’t do this for you if you use tls 😦 ).

    • pdincau
      August 8, 2010 at 12:50 pm

      hi, tanks for your hints, actually i’m on vacation this week, but i wanted to write a brief post using new features provided by last release. So thanks for your contribution! 🙂

      As you said the code seems a bit prolix..I didn’t have a chance to see the APIs, so is it only for client connection or also for gateway?

      • ppolv
        August 9, 2010 at 11:16 am

        The changes were on the client session API, the API for writing external component haven’t seen significant changes since a while

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: