How do social media apps update their interface in real time?

I was wondering how social media apps like Twitter, Facebook and WhatsApp update their UI in real time when another user interacts with the app user. To use a better example, I can think of this: when you open a chat window in WhatsApp, the UI refreshes automatically (without any user action) when the user you're chatting with interacts with you. Messages appear on your screen without updating, and the "last time" status at the top of the screen is automatically updated when your chat partner either goes offline or back online. I can think of two ways to achieve this:

  • Remote push notifications: This approach strikes me as the "cleanest" way to do it, but it's probably the riskiest way too. Using silent notifications ( content-available

    ) to send data to another device while the user is doing something is likely to save you a lot of HTTP requests and therefore consume a lot less data and CPU usage. The risk with this approach is that the user can easily disable ALL push notifications to save battery power (including silent notifications), and then your app will not be able to receive event notifications remotely.
  • Updating the local UI: This approach is by far the safest, but I think it is really "nasty" and will eventually feel like a disadvantage for everyone. Keeping the UI up to date and re-fetching data from the database to ensure the latest messages and statuses are displayed to the user would be safe in the sense that your app doesn't need to rely on device battery and background settings, but the downside is that it will make your application consume a lot of data and battery, which would be bad for the user's data plan and their device. I also don't think Apple would approve of an app that consumes that much data and power.

I have just implemented a chat feature in my application and I want to enable the same real-time UI that WhatsApp uses. What would be the best way to do this? Should I use one of the two methods above, or someone might think of another way to do this? By the way, I am a relatively new programmer who recently learned how to develop iOS (Swift) apps. I am very far from being a professional, so please easily explain the possibilities of explanations and the possibilities of work. Thank!

+3


source to share


1 answer


Chat apps use WebSockets to create a persistent connection to the client and back-end server.



This article article on Appcoda can help you start learning Socket.io. It answers your questions and also helps you create a demo application.

+3


source







All Articles