Friday, July 26, 2013

Client-Server comm using jabber in iPhone:sever setup

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 x sends a message to y user. The format of the message would be something like this:
<message type="chat" from="x@server.com" to="y@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.  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.



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.
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.

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:


We have chosen A.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.




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.

Now we are ready to dig into the development of the iOS application.