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

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

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/

Android client to client messaging without my own server for GCM

I am doing a prototype that involves messaging between clients.
What I want to do - from MyApp on device1, able to send message to MyApp on device2. Device2 should receive this and show a notification.
I don't have my own application server to push notifications from GCM to GCM clients.Is this possible ? How to do it ?
What I investigated - PubNub, which has a trial license that seems to answer my need to send messages on a channel without needing a server.
This discussion at SO didn't help much.
sending client to client messages without server interaction
Can anyone suggest better ways ?
You basically have to use a server in one way or another. I think Firebase would be perfect for your purposes. It has an Android plugin you can use that will basically alert your app when something changes in the database. Here is the documentation for that feature. Take a look at this example for implementation. Before you can use the plugin, you need to create an account and whatnot (basic one is free). Here are the instructions.
PubNub Realtime Messaging and Push Notifications
(server not required)
With PubNub, you do not need your own server to do the realtime messaging or the mobile push notifications. You likely have your own server for your database (MySQL or similar, traditional RDBMS, a No SQL DB like MongoDB, or a realtime DB sync, like Firebase) to authenticate users and such.
For realtime DB or non-DB type change notifications and signaling (any type of realtime messaging), PubNub makes is super simple to receive messages in realtime with a mobile push notification fallback in one publish action. So the GCM message will be sent along with the realtime message. If the app is actively running, you get it in realtime (< 1/4 on average - typically faster) and the GCM message will be received if the app is not active (background or not started at all).
And PubNub's free plan is not a trial (anymore). It is free (including all add-ons) for as long as you stay within the free plan limits.
See the PubNub Android docs for more details and contact PubNub support if you have any further questions with getting started or getting answers to any questions you have as you progress with your app implementation.
Another possibility is to use the SMS.
Android Send and Recieve Messages
"Send Binary (Data) SMS
We can send binary messages (as opposed to text based messages that we covered earlier) to specific application ports using sendDataMessage(). According to this Stack Exchange thread data sms is one which is sent over 2G/3G as well as GSM. I’ve tested it with mobile data turned off and it works fine charging me the same amount, so not very sure on whether it uses 2G/3G or not, but generally the term data is used in telephony when it’s related to network (tcp/ip). Anyway, SMS’s are generally sent to a specific port on the device (which is probably port 0 [zero]). But using sendDataMessage() we can send SMS’s to some other random port on which our app can listen for incoming SMSs and do something with that. In this case the default messaging app will not store the SMSs in their inbox for both the sender as well as the receiver.
"

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.

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

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

How to achieve reliable push message service?

Since I want to build reliable communication in mobile apps, could I get push failed reports(maybe device is offline) from the third party push services (C2DM, APN, urban airship) ?
Or We need to build it by ourselves?
The intended purpose of Android C2DM is to be a battery-saving way for your server app to signal the mobile device that it wants to start reliable communications.
You can structure your message so that each new C2DM encompasses everything that has occurred since the last two-way interaction with the server (i.e., "come and get whatever I've got"). Your failed delivery report is implicit in the mobile device not responding promptly (you can do this because you know C2DM activates your app with an Intent).
Is that really any worse than guaranteed delivery of each message in a lossy medium? Okay, it's worse in that you also have to implement a primary communication method. But you had to do that anyway because C2DM is inbound-only, right?
As Vinay says, MQTT may offer you the feature you desire. When a client connects to the server, it can register a "last will and testament" message with the server. If the client disconnects unexpectedly, the server sends this message to the topic it was instructed to do.
In this scheme, your client could send a message "online" to something like client//status and register the message "offline" as a LWT for the same topic. You could then have a server local client that listened to the topic client/+/status and it would know which clients were online and which offline.
I would suggest that the tokudu demo isn't the best place to look. This blog post by Dale Lane gives insight into using MQTT on Android: http://dalelane.co.uk/blog/?p=1599 and there is a review of MQTT power usage (again on Android) at http://stephendnicholas.com/archives/219
There are client implementations to suit both IOS and Android, see http://mqtt.org/software
No one service does not provide reports about failed pushes.
Failed push report makes little sence with APN/C2DM/Helium
All services are aimed to deliver the push-message under all circumstances.
If device is offline now, then push will be delivered when device becomes online.
Moreover, for iOS push-message is just a notification for a user, not for an application!
Simple case will illustrate it:
Assume that push is received when the application is turned off. In that case, notification for the user will occur. But, application will receive data from push, only if user taps on that notification! If user will tap on application's icon, then data will not be received.
So techically, push is delivered to the iOS device and application is started, but data is not delivered.
UrbanAirhip with APN and Helium
You can consider to implement your own transport for pushes. MQTT seems to be a good option.
But in this case, you have to deal with keepalives, device sleeps and battery optimisations.
All that hard work is already done by engineers from Apple, Google and UrbanAirship.
Depending on your business needs, it can be easier to adapt your architecture for existing solutions, then reimplement push-service again.
Take a closer look to UrbanAirship. In fact, C2DM has some limitations and sometimes timings for delivery of push-message is too big. Because of that UA has implemented their own transport - Helium, it works pretty good. Helium is a paid service, but UA provides good SLA.
I am suggesting for push notification IBM MQTT protocol. This fine enough for push notification.
see the demo from https://github.com/tokudu/AndroidPushNotificationsDemo
I did something similar where I had a database keep track of push queues to known subscribers and had reporting when failed. It was very simple and went something like this...
The schema was like so:
pushMessages
messageID , GUID, PK
message , nvarchar (256),
expires , datetime
messageQueues
subscriberID , GUID, PK
messageID , GUID PK
failedPushMessages
subscriberID, GUID, PK
messageID , GUID PK
(subscriber table omitted)
Once a client received the message successfully, the client would ping back to the push server and notify it via the unique queueItems ID that it received on the push notification. There would also be a daily database process that would check for expired push messages. When found, it would do a join on the queueMessages matching the messageID then remove them from the messagesQueues table and copy them to the failedPushMessages table.
This was very easy to understand and maintain but I don't have experience doing it another way.
Push Services are an efficient and reliable way to alert your users. They allow even background applications to inform users of new information in real-time. Push Services are widely used for a variety of fields in mobile applications, such as weather updates, messaging services, mail notification, coupon services, and so on. Push Services are no longer optional but have become essential.

Categories

Resources