How to make a chat application in android?

Urvashi Hirani picture Urvashi Hirani · Dec 28, 2010 · Viewed 43k times · Source

I would like to add chat functionality with online user in gmail in android. how to add chat feature in my own application in android?

I want to show an online user in google maps which is nearest to the area which I have selected and have the ability to chat with that online user

Answer

James Andrews picture James Andrews · Jan 20, 2017

To add chat to an Android application there are a number of options. I'll present the options ordered by the amount of development expertise that's required - from least to most.

Using a real-time backend service

There are a number of companies offering real-time backend services. These services would provide you with an SDK which allows your app to access their servers. Using the SDK you would be able to stream data between devices.

Here is a list of the most popular services:

Firebase

Google owned real-time database which uses WebSockets and MongoDB as it's core technology stack. The service allows you to save data in a no-SQL database and then register to receive real-time updates when that data changes. They also offer services for push notification and file storage. There is also a third party open source messaging framework available for iOS and Android.

Firebase have a library called GeoFire which allows you to make your apps location aware.

Pusher / PubNub

I've grouped these together because they are quite similar. They provide real-time streaming infrastructure and technology. You can establish channels between groups of devices and stream data between them. They don't provide any means to authenticate users so you would need an app server.

Quickblox

Quickblox is targeted specifically at the instant messaging market and they use an XMPP server on the backend. Since they use XMPP, this service has a lot of messaging features built in like privacy lists and typing indicators. They provide a reasonable but basic example messaging app.

Layer

Layer is half way between Quickblox and PubNub / Pusher. They offer a streaming platform which is targeted towards instant messaging. They have some messaging features like basic blocking but this isn't a full scale messaging implementation like Quickblox. They have a library of UI components that can be used to help build a complete app - Atlas.

If you use a backend as a service you will always have to build the chat client yourself. If you go with one of the more general purpose options like Firebase, you will also have to build your own messaging protocol.

These services will save you time when it comes to the real-time server implementation, server configuration and scaling.

XMPP

Another option is to host an XMPP server yourself. There are a number of open source XMPP servers available:

XMPP servers will provide you with a large amount of messaging functionality out of the box. They generally don't include any location awareness but this could be solved by configuring the server to host the user database on a separate server - that would allow you to add extra tables to handle the geolocation. An alternative would be to use presence to update relevant devices when a user's location changed by a certain amount.

To build the client I would recommend using Smack. Since XMPP is an open standard, your client should be able to work with third party XMPP servers and clients.

If you don't want to build the client yourself there are a number of open source projects (mostly under copy left licenses) which could act as a starting point. Xabber would be one example but you can find more by searching. Alternatively, you could use this a commercial open source project.

Building the server yourself

The final option is to build the messaging server yourself. This has the benefit that you could add the features you need and keep it reasonably simple. A good approach would be to use Symfony and the WebSocket Bundle for the real-time back end and Android WebSockets for the client. Using this approach you would have a lot of flexibility implementing the geo location functionality.