Yowsup is a python library that allows you to do all the previous in
your own app. Yowsup allows you to login and use the Whatsapp service
and provides you with all capabilities of an official Whatsapp client,
allowing you to create a full-fledged custom Whatsapp client.
A solid example of Yowsup's usage is Wazapp. Wazapp is full featured
Whatsapp client that is being used by hundreds of thousands of people
around the world. Yowsup is born out of the Wazapp project. Before
becoming a separate project, it was only the engine powering Wazapp.
Now that it matured enough, it was separated into a separate project,
allowing anyone to build their own Whatsapp client on top of it.
Having such a popular client as Wazapp, built on Yowsup, helped bring
the project into a much advanced, stable and mature level, and ensures
its continuous development and maintaince.
Yowsup also comes with a cross platform command-line frontend called
yowsup-cli. yowsup-cli allows you to jump into connecting and using
Whatsapp service directly from command line.
This is a simple IM application runs on Android, application makes
http request to a server, implemented in php and mysql, to
authenticate, to register and to get the other friends' status and
data, then it communicates with other applications in other devices by
socket interface.
EDIT :Just found this! Maybe it's not related to WhatsApp. But you can use the source to understand how chat applications are programmed.
There is a website called Scringo. These awesome people provide their own SDK which you can integrate in your existing application to exploit cool features like radaring, chatting, feedback, etc. So if you are looking to integrate chat in application, you could just use their SDK. And did I say the best part? It's free!
*UPDATE : * Scringo services will be closed down on 15 February, 2015.