Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does apps like whatsapp show contacts that are only using the app?

In apps that use phone authentication like whatsapp:

When any user opens to check his/her contacts, whatsapp will show only the contacts from your phone that are using whatsapp (like they filter your contacts).

My thoughts on implementing something similar:

I can save all users in a database (when they authenticate with my app), now when I load contacts in an activity I check to see if the contact exist in the database, and if the user exist then I add him/her to the list.

Problem:

If I used the above method:

Then lets say I have 10,000 users in the database, then it wont make sense to loop through 10,000 users every time I want to show contacts of a user inside my app.

Question:

How do you think apps like whatsapp implement such a thing?

like image 417
data Avatar asked Aug 11 '18 15:08

data


1 Answers

You have to loop through your users with contacts at least one time but you can do it smartly just like WhatsApp do.

When user first time open and register in WhatsApp they ask for your permission to read your contacts (From API Level 23) , when user grant the permission WhatsApp in background connect to their backend server (where they save their all user's data) with the contacts available in user's device and in response of this http request only those numbers returns with their account info which are registered on WhatsApp and then WhatsApp locally they save them user's device (SQLite or any database related service) and when user open WhatsApp's contacts activity it shows data from the local user database not the server.

Now question is : What happen when new Contact is Added?

WhatsApp will be notified if user created new contact and then WhatsApp again connect to their backend server with the new contact's number. Now suppose if user was not connected to the internet when creating the new contact then what happen?

WhatsApp will save the number to another table of their local database as not checked number and then once again when user will connect to internet whatsapp will again connect to their backend server with same type of request. Also in WhatApp's contacts activity there is a option called force refresh which do the same thing but not in background.

this is same for deleting a contact except there is no need to connect to server it actually deletes the WhatsApp contact from user local database.

Now question is what are the benefits of this?

  • Small data uses which is great for user experience
  • No need to contact to server every time user open the contacts activity. Which is also great for user experience.
like image 82
Sayan Roy Avatar answered Oct 19 '22 07:10

Sayan Roy