转:Jabber Client for iOS: Server Setup

Building a Jabber Client for iOS: Server Setup

 
 

转自:http://mobile.tutsplus.com/tutorials/iphone/building-a-jabber-client-for-ios-server-setup/

 

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:

1
2
3
<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

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

posted @ 2013-11-26 21:41  我有一个梦想  阅读(447)  评论(0编辑  收藏  举报