Get $500+ of the best After Effects files, video templates and music for only $20!
Building a Jabber Client for iOS: Server Setup

Building a Jabber Client for iOS: Server Setup

Tutorial Details
  • Difficulty: Beginner
  • Technology: iOS SDK
  • Completion Time: 30 Minutes
This entry is part 1 of 4 in the series Building a Jabber Client for iOS

In this tutorial we will build a Jabber Client for iOS. The application developed in this series will enable users to sign in, add buddies, and send messages. In the process of building this app, we will describe how to install and configure a jabber server, create accounts, and interact with the server from an iOS application. To simulate a chat environment we will build a scenario with two users: one user will be chatting from an iPhone app and the other will be using iChat.

What is Jabber?

Jabber is not a tool but a community which builds and maintains XMPP. XMPP stands for eXtensible Messaging and Presence Protocol. Such a protocol is open-standard and oriented to message exchange. The original name of the protocol was Jabber, so the terms are often used interchangeably. Message exchange happens near real time, so it is an ideal infrastructure to build chat-like applications. The protocol also implements a mechanism to notify presence information (whether a user is online or not) and the maintenance of a contact list. XMPP is a thorough protocol, which has been adopted also by big companies like Google to build their Instant Messaging service.

How Does it Work?

The XMPP protocol is based on XML (Extensible Markup Language) so each type of message (e.g. login, message send, etc.) is encoded in this format. For example, let’s suppose President Obama sends a message to Hillary. The format of the message would be something like this:

<message type="chat" from="obama@server.com" to="hillary@server.com">
	<body>Hi there</body>
</message>

For sake of completeness we should mention that XMPP is a decentralized service, much like email. This means that the Hillary and Obama accounts might be on different servers (i.e. obama@server.com and hillary@otherserver.com). The message is delivered anyway because XMPP enables server-to-server communication. In case Hillary is offline, the message is cached on the server and delivered when she goes online. To keep things simple and focus on the client side, in this tutorial we will consider a scenario with just one server.

Installing Jabber

There are many implementations of Jabber servers. A pretty complete list is available here: Xmpp. In this tutorial we will use jeabbered for it is easy to install and configure. Ejabbered is developed in Erlang, it is opensource and can work on many operating systems, including Mac OS X. The choice of such an implementation is also due to the easy web interface, which allows to quickly configure the service and manage user accounts. Ejabbered is available for free here. We select the Mac OS X version and start the download. In this tutorial we will use the version 2.1.8.

Jabber Client

The installation process is very easy and intuitive. In fact the downloaded file is an application which assists us in the installation process by means of a wizard. Once completed we should have a folder named ‘ejabbered-2.1.8′ in our ‘Applications’ directory. The folder should contain the following subfolders.

Jabber Client

The most important folders are ‘conf’ and ‘bin’. The first contains configuration files to administer users’ privileges. The second includes commands to manage the server. Let’s open a terminal and move to the bin folder. We should see something like this.

Jabber Client

The most important commands are ‘start’ and ‘stop’. In case they are not executable you can make them so by issuing the following shell command:

chmod 755 stop
chmod 755 start

The folder contains also a ‘postinstall.sh’ script, which has to be run right after the installation to create the admin user. The script expects three parameters: user, domain and password. So we can run it like this:

./postinstall.sh cesare jerry.local password

We have chosen jerry.local, the name of the local machine, as domain but ‘localhost’ would work as well. This starts the server and adds this user as administrator. To double check if the configuration is correct we can open the ‘ejabberd.cfg’ file in the ‘con’ folder. In the Access Control List section it should contain the following
statement

 {acl, admin, {user, "cesare", "jerry.local"}}.

Now you should be able to start the jabber server by typing

./start

This will open by default a web page which notifies you that the server has been started.

Jabber Client

Configuring Jabber Users

At the moment our service has just administrators. We need to populate it with at least one user. The previous web page contains a link to the admin interface, which is available at http://localhost:5280/admin/. Once you have logged in as the admin you should see a console like the following

Jabber Client

We click on the “Access Control Lists” item and the list of currently registered users appears. We create a new user, named ‘alterego’, by entering its details and clicking submit

Jabber Client

Testing the Server

Now we are able to test the server and check that it works correctly. To test it we can run two desktop applications which support the XMPP protocol. One is iChat, which comes with Mac OS X. You can find another, the one that suits you better, from this list:

http://xmpp.org/xmpp-software/clients/.

In our case we will use Adium, which is available here. We will configure an account for ‘cesare’ on iChat and another for ‘altergo’ on Adium. Let’s start with iChat. Start the application and open the Preferences menu and select the Accounts tab.

Jabber Client

The “+” buttons allows adding a new account, which we fill with the following data:

Jabber Client

The first connection might take some time (e.g. 30 seconds). iChat will probably ask you to accept the unknown certificate which is bundled with the server. Just click continue.

Jabber Client

Once you are connected you can change your status by means of the drop down menu at the top of the window. Let’s set it to online. Now let’s move to Adium. We start the application and we open the Preferences menu. Adding a new account is pretty similar to iChat. Here we enter the ‘alterego’ details.

Jabber Client

In this case we have also to specify the server which is running jabber. We select the ‘Options’ tab and we fill it as follows:

Jabber Client

Adium will ask you to accept the certificate as well. Now we have to two users connected to the server by means of two different applications. This should also be reflected in the log file of ejabbered. The file is located at: ‘/Applications/ejabberd-2.1.8/logs/ejabberd.log’ and should contain the following messages

Jabber Client

Chatting is about friendship. Although both users are connected to the server their are not enabled to chat unless they add once each other as friends. The scenario will be the following: cesare adds alterego to his list. In the iChat window we select the “+” button at the bottom.

Jabber Client

and we enter the jabber id ‘altergo@jerry.local’

Jabber Client

On Adium we should receive an Authorization Request as follows

Jabber Client

Once alterego accepts it we should end up as in the following figure, cesare is friend with altergo (and vice versa) and both can exchange messages.

Jabber Client

We have tested all the functionalities needed for the server:

  • login
  • buddy list building
  • message exchange

This was needed to ensure that possible errors in the iOS application are not due to server misconfigurations. Now we are ready to dig into the development of the iOS application.

Source Code

The complete source code for this project can be found on GitHub here.

Series NavigationBuilding a Jabber Client for iOS: Interface Setup»

Add Comment

Discussion 25 Comments

  1. Andre Dublin says:

    This couldn’t come at a better time, I’ve got to create a jabber server for my client sans mobile, but thanks for the tut!

  2. CRocchi says:
    Author

    Glad to hear it’s useful to you!

  3. moosc says:

    Great!!!!!

    I hope that you use this framework
    http://code.google.com/p/xmppframework/

  4. David Banes says:

    Brilliant series of articles. As an option to setting up your own server consider hosting services like Cleartext for business use or Cleartext.IM for individual user accounts.

    We’re giving away accounts on Cleartext.IM at the moment, mention this article to get one complete with gateways to MS Live, Y! and AOL Messengers, even if we’ve closed the free accounts we’ll give you one up until the end of September 2011.

    David Banes
    (past XMPP Standards Foundation Chairman :-)

  5. Steven says:

    Thanks for the great tutorial.
    But, can ejabberd run in Mac OS Lion? In my machine, it can’t run the Post Install Shell.

    • Author

      I built the tutorial on Snow Leopard and I did try to install ejabbered on Lion.
      Alternatively you can try another XMPP server which is Lion compatible.
      BTW I am one of those who think it is better to postpone the upgrade to Lion: http://spreadingfunkyness.com/dont-upgrade-to-mac-os-lion-not-yet/

      • vikas Ojha says:

        Hi

        i tried installing ejabberd -2.1.8 and 2.1.9 versions om Mac but none of them gets installed and shows post install script error .I dont know i tried installation 2 months earlier at that time ejabbered 2.1.8 got installed on seperate Mac min iand i was able to run these commands also on terminal.Any suggestion would be appreciated.I did it on Snow leopard.

        Thanks

  6. vikas ojha says:

    HI Cesare,

    I have downloaded the ejabberd2.1.8 and ran the command

    cd /Applications/ejabberd-2.1.8/bin> ls

    that shows me the list after that when i run the postinstall command i get a error message

    chd-saber-2:bin vikasojha$ ./postinstall.sh admin vikasojha Mi123

    -=- ejabberd post installation script -=-
    (c) 2005-2010 ProcessOne

    * Checking ejabberd installation

    * Starting ejabberd instance

    * Creating administrator user
    Can’t register user admin@vikasojha at node ejabberd@localhost: not_allowed

    Commands to start an ejabberd node:
    start Start an ejabberd node in server mode
    debug Attach an interactive Erlang shell to a running ejabberd node
    live Start an ejabberd node in live (interactive) mode

    * Stopping ejabberd instance
    Killed
    ==> Setup finished

    Please tel me if i did any thing wrong.

    Thanks
    Vikas

  7. enroyed says:

    Hi, I can not install ejabberd 2.1.8 on mac OS x lion.it can’t run the Post Install Shell.Same result for windows 7 on intel macBook. What can I do ? I need to run and test ejabberd urgently…

    Thanks

    • Vikas Ojha says:

      Hi

      i tried installing ejabberd -2.1.8 and 2.1.9 versions om Mac but none of them gets installed and shows post install script error .I dont know i tried installation 2 months earlier at that time ejabbered 2.1.8 got installed on seperate Mac min iand i was able to run these commands also on terminal.Any suggestion would be appreciated

  8. Xu says:

    Hi!!!

    I have two questions….

    I can chat… i connect to chatme.im and i use the iphone simulator for one client in my macbookpro and the iphone simulator on my mac to chat …
    I tried with your code but a use a openfire server… And always the connection of TurnSocket fail
    And I dont know why… I thoutgth that openfire doesnt support SOCKS because, TURNSocket is an implementation of XEP-0065: SOCKS5 Bytestreams and I need to setup a SOCKS5 proxy server.
    However, openfire can works like a proxy server. Why i cant connect !! I’m going crazy XD
    Maybe with the iphone simulator can use the TurnSocket ….

    If a cant use TurnSocket class how i can transfer an image between two clients???
    Because, i think that i cant connect 2 clients if a dont have a proxy because what happen if one of the clients use a nat address..

    I find the only solution is to upload the image to a server and send the url, but this solution is ugly :(

    This is my email : kientienemibarraespaciadora@gmail.com

    Thanks !!

  9. sai says:

    i am use iphone simulator to send message that will display in console TurnSocket fail connectinon fail how plz tel me the turnsocket process

  10. Just want to say your article is as amazing. The clearness in your post is simply excellent and i could assume you are an expert on this subject. Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please carry on the rewarding work. This is really interesting, You’re a very skilled blogger. I’ve joined your rss feed and look forward to seeking more of your wonderful post. Also, I’ve shared your site in my social networks!

  11. Tharun says:

    Thanks for the effort to share… Good Job.

  12. Ashraf says:

    Hi,
    I was trying to install the ejabberd2.1.10 on my mac machine was not able to install it correctly, please guide me to set this thing up so that i can test the tutorial.

    With Regards,
    Ashraf

  13. Tarun Sharma says:

    Hi,

    First of all thanks for posting such an important module.

    I followed the steps and got it installed & started server. But i’m not able to configure/add user through Adium & ichat. It asks for password every time but do not connect.

    I found the following lines in Log file:

    =INFO REPORT==== 2012-03-30 06:52:40 ===
    I(:ejabberd_listener:281) : (#Port) Accepted connection {{127,0,0,1},49819} -> {{127,0,0,1},5222}

    =INFO REPORT==== 2012-03-30 06:52:40 ===
    I(:ejabberd_c2s:802) : ({socket_state,tls,{tlssock,#Port,#Port},}) Failed authentication for baba@tarun.local

    i don’t what i am doing wrong. Please guide me.

    Thanks,
    Tarun

    • Tarun Sharma says:

      Guys,

      Got it working at my workplace.. The reason behind why i was not able to initiate was that i was trying sign in using wrong id.

      But one issue is still while i was trying it at home again. when i configure account using Adium it simply authenticated using same steps but not with iChat. It (iChat) din’t ask me to accept the certificate. Might be i am making a silly mistake again, not sure. However, i got it working at my workplace.

      kindly let me know what i might be doing wrong.

      Cheers,
      Tarun

  14. obaid says:

    My problem is also the same i have created the account in iChat and the iChat account is the admin account and I have the password for the admin account that is the reason i was able to create the account in iChat but friends when I tried to create an account in Adium, with the other test user which I have created in Access control list and tried to register it in Adium it always prompts me for the password what is the issue, i don’t understand this and it is logical too, when i have created the admin account for the server i have registrered it with the password but what about the testuser for Adium. Please help…

    • GuoChen says:

      I also have the password problem, why i did not have to enter a password when I created a new user in the ACL

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.