Android: How to let a server know a user is still online? - android

I'm building an android app where the server needs to know if the user is still online or not.
You can compare it with facebook chat on android. If the user is on the app, other users can see he is online. But when the user loses internet connection or closes the app the server needs to know he is not online anymore.
What is the best way to do this?

It depends on your server type. For example if you use sip you can do this by sending registration message before expire time.
But in general the common practice is to send a keep_alive message within a short interval. if you use any third party standard server then read their documentation and apply that way. If you are planning to create your own server system then just create a custom keep_alive message format and send it to server at regular interval.

#Jeeten Parmar
An easy way to implement a keep_alive system is by using MQTT.
MQTT is a machine-to-machine internet protocol.
http://www.eclipse.org/paho/
When you implement Paho in an Android project, you can subscribe to an MQTT server.
When the client disconnects from the internet, an event can be called. You can even set a lastwill message.
You can also set the keep alive interval. ->
http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#keep-alive-timer

Related

Do apps like WhatsApp keep an always live connection with server?

I'm creating an application that has to realtime be live with the server. I'm not sure whether to have an always live (or checking with server every 2mins) app because it might drain the battery. What are my alternatives?
WhatsApp uses XMPP (eXtensible Messaging and Presence Protocol) to handle the message delivery system.
XMPP is mostly like HTTP where the client opens the socket with the XMPP server and keeps it open as long as the client is logged in. It's not like the regular REST API where the client opens the socket send/receive the data and close the socket. The socket is opened as long as you are signed in. In case of WhatsApp that's eternity (not really, WhatsApp reconnects automatically if the connection terminates)
As written, use push notifications (working even with closed app) or socket connection (working with app on the background). If the app is on foreground, you can use whatever you want - screen will take most of the battery...
Whats app using XMPP server, as like mentioned in above answer. For free of cost, If you want to create a messenger application like whats app. Tou can use some of the free real-time databases are available in the market like "FireBase" server.
You can get sample code here: https://codelabs.developers.google.com/codelabs/firebase-android/#0
and further information regarding the firebase server and required libraries you can find here: https://firebase.google.com/docs/database/

How Messages are Received on WhatsApp or any other messaging service?

Can anyone please tell me how whatsapp messages are received on my mobile? Like, does my phone requests whatsapp serve every second or does the server send my phone a ping when a message for me is received at the server end. I don't want to know the exact procedure, but an overview will do the job.
Well i guess, its just not that easy to answer in this scope however here is how it might work.
For this you may need understanding of technology on mobile side and server side.So let me try to explain it in a very simple manner which may be the implementation.
1) No, your mobile does not ping the server everytime. Polling like this adds lots of overhead on the network, also multiple calls can be stacked up if there is delay in responding thus leading to huge traffic on server side. Instead , its other way round. Whenever the server recieves any msg it pings you instead to say that you recieved a msg.
2) Hope point 1 makes it clear that its other way around. Now would like you to know about push notification. Push notification is something by whichserver can notify mobile of any new msgs or information or update.Here's a link which can give more detail on push notification for android https://parse.com/tutorials/android-push-notifications
3)How the server processes the request. This itself is a big topic. Basically it goes like this.I assume there are two users, A and B
First Mr A send a msg to Mr B
Server recieves this request with information of the user A(i.e the person who sent the msg) and user B(i.e the person to whom the sg is to be sent)
The server does some processing to get to know the user id of Mr B and other infomation about Mr B and then does a push so that user B can recieve this notification.
Of course a lot of technology is involved to do this job in order to handle millions of concurrent connections and responding fast without blocking other connections.
If you want to have a basic view of the technology, i would suggest you to look at socket io and node js. Heres a chat example
http://socket.io/get-started/chat/
Hope this helps
WhatsApp uses XMPP (eXtensible Messaging and Presence Protocol) to handle the message delivery system.
XMPP is mostly like HTTP where the client opens the socket with the XMPP server and keeps it open as long as the client is logged in. It's not like the regular REST API where the client opens the socket send/receive the data and close the socket. The socket is opened as long as you are signed in. In case of WhatsApp that's eternity (not really, WhatsApp reconnects automatically if the connection terminates)
XMPP protocol has been used in various chat applications such as Google Talk, Facebook messenger etc.
As far as actual technology goes, WhatsApp uses heavily customized version of Smack library on Android to build their client and uses customized eJabberd Server to handle the XMPP traffic. They might have different backend solution for handling the data though which might be on one of the cloud storage/computing network (I think it's heroku, no real idea though).
On iOS and other platforms, I suppose they might have developed their own libraries. Developing own libraries is not a lot of work, especially when you have customized needs and have a team of developers. I have used one of the libraries available for Windows Phone and heavily customized them to work for us. I made some improvement on the library, but due to time shortage I couldn't submit them to the original repo (Documentation is really tough).
Anyways, if you are interested in learning the tech, you can read Oriely's "XMPP: The Definitive Guide" and can visit The XMPP Standards Foundation.
Basically to say that when there is a message, Server will sends a notification to Client via Push Service. In before Client must register to use Service with Server, Server will provide an Id for Client and then uses it to specific which Client will receive the message.
Read more in Here and Here

send and receive notifications in android

I am developing an app in android in Eclipse IDE. The app modality requires that any user can challenge his/her friend to do a certain activity. Whenever a user does so, the friend will immediately get a notification in his/her phone. Even more, the user who challenged the friend, will also receive a notification when the friend accepts the challenge. Is there any particular way I can provide this functionality? I know how to generate notifications. But the server side interaction is quite confusing. Should I check the server database through the app at frequent intervals and check for such activities in database? I think the procedure will hamper the performance of the app. I would be very grateful if anyone can suggest a good way.
But the server side interaction is quite confusing.
As you're pointing out, it's the server part not the Android one what's confusing here. If you don't have any constraints on the server part, I'd recomend to use Socket.io http://socket.io/ They have a tutorial for connecting socket.io with Android: http://socket.io/blog/native-socket-io-and-android/
It seems that you don't really need an interaction between the differents users but only a way to notify them. I always used Google Cloud Messaging to do so. It enables you to receive push notifications. As you said, checking the server periodically is battery/data consuming and not the best way to do it.
This link might help to set up GCM : http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/.

XMPP (Openfire) Push Notifications Android

I really tried to find a solution to this problem but didn't. There are some threads related to this concept, but I didn't find a comprehensive solution.
I have a server with Openfire which sends messages. When both users are online, there is no problem. However, when a user is offline I would like to send a push notification, as in Whatsapp. I heard that Whatsapp and other IM apps use a modified version of ejabber to send push notifications, but couldn't find out how.
One possibility is to use GCM, but I am not sure if this is the right way to go. Also I would like to avoid polling, for the sake of not draining battery life.
Does anyone know some solution to this or has an example?
I am studying the openfire and found some good options that I would like to share with you.
I found a plugin in github and i will test it this week. It seems that will be a good solution to your goal and to my: https://github.com/meisterfuu/Openfire-GCM/
Another option is to use CallbackOnOffline Plugin that calls a specified URL with To and From parameters. This url could be a endpoint in your web service, and with that data (To and From) you could get the associated Id for push in your base, and did a push.
https://www.igniterealtime.org/projects/openfire/plugins.jsp
Well, you could use GCM with XMPP.
The new GCM Cloud Connection Server allows you to use the XMPP protocol for a bidirectional (Cloud to Device and Device to Cloud) communication between your server and your Android application.
The GCM Cloud Connection Server (CCS) allows third party servers to
communicate with Android devices by establishing a persistent TCP
connection with Google servers using the XMPP protocol. This
communication is asynchronous and bidirectional.
In openfire its done automatically, messages will be recieved by the user when the user gets online. So no worries send message to an offline user and when the user comes online message will automatically pushed to the user.

Sending updates from server to android

What is the recommended way to send updates from a server to an Android app? I need to send data from the server that will show up in the Android notification list.
Polling the server is one option. But is there a better way? I have a website that receives updates from the same server using HTML5 Server Sent Events. Is there a way of doing something like this in an Android app, ie. creating a persistent connection that listens for server events?
Anyone know the best practise way of implementing this functionality in an Android app?
Have a look at Google Cloud Messaging, this is a free service for android phones that allows server pushing. It allows you to push messages , notifications or data.
Its very hard to say if theres a best practice , it all depends what you are doing. But If you follow the example in the guide , it should be fine.

Categories

Resources