Sunday, 15 July 2012

Create your own Chat system

Swami Nikhilaananda said:
"Chat is cool, chaat is cooler"

If you have used Pidgin, you would have seen the word XMPP. It will be really great if you have a look at this beautifully written article by Adarsh Ramamurthy on XMPP.

In this post, we attempt to communicate between two computers via chat - And no, not using Google or Yahoo :-)

I am using Linux on both machines. You can do the same thing with Windows.

Step 1, is to download the XMPP server. Let's call the machines as A and B. I stumbled upon ejabberd long back, and so used that. There are alternatives as well. On machine A, download and install ejabberd. I simply ran:
sudo apt-get install ejabberd
If everything went well, you will have it up and running, no sweat.

Now, in step 2, we will create two accounts. To do that, we will register them using the following commands. Oh, I am going to use Romeo and Juliet just for continuity from Adarsh's article.

sudo ejabberdctl register romeo localhost loverboy
sudo ejabberdctl register juliet localhost hotbabe

This is equivalent to "Creating an account". User name romeo has password loverboy on domain localhost. If you now run the following command, you will be able to see all registered users:
sudo ejabberdctl registered-users localhost
Let's proceed to Step 3. Install Pidgin on both machines (You can use other chat clients like Empathy as well). Once Pidgin is installed, on machine 1, you can just go to Accounts  >> Manage Accounts. Add a new account. Choose XMPP for the protocol, romeo as username, loverboy as password (optional) and enter localhost for Domain. Open a terminal and type in ifconfig, and make note of the IP address. Done!


Now go to machine B. Again, Accounts  >> Manage Accounts and enter Juliet's details in the Basic tab. Now, switch to the Advanced tab, and type in the IP address of machine A that you had noted down in Connect Server. Save it. Done!

Now test it out. In the Pidgin Window of machine B, click on the Buddies menu and click on New Instant Message. Enter the name romeo@localhost and press OK. In the chat window, enter "Oh Romeo, my Romeo, where art thou my love" and when it beeps on the other computer, pat yourself on your shoulder!

This is of course extensible to anything else... Just to check that, let's try this out with PHP. Download the XMPPHP Library. I am using the standard code that they have provided here below. The commented line is what they had provided, and that works for Gtalk - so you can easily implement that. The uncommented one, is for your custom chatting application. This of course is one message because that's where I stopped. Stay tuned for continuous chat and more...

Note that the ID of Juliet has some big number - pick that from the Pidgin chat window as is...

..

..

[This post is not complete at the moment, but is complete enough to give you a rush :-) If you are that kind of a person]

No comments: