Developing a chat application using PubNub

Developing a chat application in Android is tough. You need to have your own chat server, which is a pain to setup. It also requires your android app to handle your own HTTP requests and responses which requires AsyncTask. But to some relief we have Volley, Retrofit, Ion and other third party libraries to simplify the process. What if we don't have to do anything mentioned above and still get a super powerful chat application. Well that is where PubNub comes.

Android chat application using PubNub

WHAT EXACTLY IS PUBNUB?

According to PubNub

PubNub is a secure global Data Stream Network (DSN) and easy to use API that enables our customers to build, scale, and manage realtime functionality for their applications and IoT devices.

Now you might be asking what is realtime? When someone sends you a message in a chat app, and you receive that message on your phone, or you get a push notification, that notification or message is received in ‘realtime.'

So why we are using PubNub? Duh. We are developing a chat application which is meant to be realtime.

LET'S GET IT STARTED

First we need to import the required libraries.

compile 'com.pubnub:pubnub-android:3.7.5'
compile "com.android.support:recyclerview-v7:23.0.1"
compile "com.google.code.gson:gson:2.3.1"
compile 'com.android.support:appcompat-v7:23.0.1'

We are using Gson for converting our String message to JSON, and then decoding the JSON message to String in the receiving side.

For a chat application, we require a login page and a chat window. This is done with the help of Fragments. I am using an Activity which inflates LoginFragment if the user is not logged in, or it inflates ChatFragment if the user is already logged in. Keeping track of user login information is done by SharedPreference. Whenever the user logins, username is stored in SharedPreference under the key 'username', and when the user logouts, it's removed. CustomCallback is an interface, which tells the MainActivity to switch the fragments whenever required.

PUBNUB SDK

SUBSCRIBING AND RECEIVING MESSAGES

First we have to initialise the SDK with PubNub's publish key and subscribe key. Then the next step is to subscribe to a particular channel. Channel is one of the most important part of PubNub. Once subscribed to a particular channel, you can sent and receive messages through a particular channel. One or more devices can connect to the same channel simultaneously at the same time.

Once subscribed to a channel, you can have the following callbacks, which you can override depending upon your needs.

public void successCallback(String channel, Object message)
public void successCallback(String channel, Object message, String timetoken)

Whenever you receive a message, this is the method which is called. String channel is the channel name, and message is the message that is sent through the channel. The timetoken response value is a string, representing 17-digit precision unix time (UTC).

public void errorCallback(String channel, PubnubError error)

When there is an error in the connection, this method is called. PubnubError error refers to the error.

public void connectCallback(String channel, Object message)

Called when the device successfully connects to a channel.

public void reconnectCallback(String channel, Object message)
public void disconnectCallback(String channel, Object message)

reconnectedCallback() and disconnectCallback() are called when PubNub reconnects and disconnects from a particular channel.

400: Invalid request

SENDING MESSAGES

To send messages through a particular channel, PubNub provides you the publish() method, which has three parameters. Channel name through which the message is to be sent, the String message and finally the Callback object. There is a callback for both success and failure.

In my case, since I have to send message along with the sender name, I am encoding the message into JSON format and then sending the JSON object as the message. PubNub has an overloaded publish() method which takes in a JSONObject. So for example, if my username is capt.swag and message is "Hi, how do you do?", then the corresponding JSONObject will be

{
    username: capt.swag,
    message: "Hi, how do you do"
}

I'm using Gson for encoding and decoding messages, to and from JSON object. Gson has simple methods toJson(Object object) which converts the message to JSON, and fromJson(Object object) which converts the JSON to it's original form. Both methods takes a POJO (Plain Old Java Object) argument.

400: Invalid request

PUBNUB CHAT APPLICATION [DEMO]

FINAL THOUGHTS

It was fairly simple to setup a chat application using PubNub. PubNub is a super fast and reliable service for building softwares which require realtime messaging. They have SDK's for more than 70 programming languages. So if Android is not your platform, I'm quite sure you are still covered by PubNub. PubNub has a cool YouTube channel where they do demos and podcasts.

The entire code is available over at GitHub Also follow me on GitHub to see my latest projects.