3

I am interested in the procedure behind the handling of inbound messages and notifications from instant messaging apps such as Telegram and WhatsApp. I am acquainted with the push protocol, but I am curious about how instant messaging apps implement the receiving part.

First, are instant messages from services like WhatsApp and Telegram received in the form of a push notification, or is there duplication/redundancy giving rise to some sort of a race condition between push notifications proper handed to an app instance service worker, and messages handed to an app instance foreground/main processes? Alternatively, are instant messages always and only sent as push notifications, as least for end-to-end encrypted messages from apps like Whatsapp?

Secondly, under the duplication/redundancy hypothesis mentioned above, is the notification handled by the service worker passed over to the app instance, which then displays it in the target chat, or is the notification discarded in favour of the app instance's fetching the original message from Telegram/Whatsapp server? (I have in mind a scenario where this is required to ensure that the sender is provided a reliable confirmation that the message has been received.)

WhyNotTryCalmer
  • 357
  • 5
  • 15

1 Answers1

3

Actually it's a tricky question cause apps like Telegram and WhatsApp probably have some multiple cases handling logic, very complex logic.

But from what I can imagine and actually used in some of my chat apps is the following:

  • Some real-time protocol is used for messaging. Telegram uses their own proprietary protocol, WhatsApp uses XMPP.
  • when both parties are online (sender & recipient) then a real-time XMPP message is delivered. Mainly & usually an app has a persistent TCP/TLS/WSS connection to chat server, mainly while a user uses it.
  • when a recipient is not online (not connected to chat server) e.g. does not use an app (an app is in background/suspended mode) then a push notifications will be delivered. And this is only to notify a user that there is a new message. Then a user (recipient) opens an app and receives a real message (via real-time XMPP connection) or via sync with server by REST/HTTP API.

So there is no any duplications since pushes are just to notify a user there is a new message while a user is not in app.

Rubycon
  • 18,156
  • 10
  • 49
  • 70
  • thanks very much for your extensive reply. Quick remark about your § "when a recipient is not online [...]". From what I've observed both Telegram and Whatsapp are able to display the content of inbound messages before the user opens the app. This means that the push notification must either have some payload packaging the content of the message to display, or that the push notification also wakes up the app before the user does, contrary to what your reply suggests, to display content before the app is in the foreground. Do we agree on this? If yes, which of the two is common practice? – WhyNotTryCalmer Jan 27 '19 at 22:17
  • Right, probably they use push notifications payload to show message as quicker as possible. Nowadays push payload has a big size, so lot's of info can be passed in it. I mean it's possible. This approach can be used as well. – Rubycon Jan 30 '19 at 08:25
  • Great explanation. I want to know who handles the push notification and how? For example: When the app is suspended, does it still have connections to the server to recieve the notification? Or does the whatsapp server sends the notification to the android / google layer which then in turn invoke the required app? – Vivek Kumar Feb 24 '21 at 15:21
  • @VivekKumar push notifications are handled using standard OS push notifications API, iOS and Android. – Rubycon Feb 28 '21 at 12:23
  • @Rubycon, yes that I understand. But what I dont understand is, does the chat app keep looking for new messages by connecting to server and if new message is present, send the notification to the system. Or the chat remote server sends some kind of notifitication to the android device (without the chat app being active) and how? – Vivek Kumar Mar 01 '21 at 07:13
  • @VivekKumar for this, basically, an xmpp server (on each new message) should check whether a recipient is online or offline. If offline - then initiate a push notification instead of real time message. Many XMPP servers support it already, and for some - it will require to customize a server code – Rubycon Mar 02 '21 at 08:22