NeiL
NeiL

Reputation: 801

Realtime userlist in android App

I am creating a chat app in android. App will be having 100-150 users at a time(changing realtime). Users data is come from http webserver. I am trying to create a realtime userslist which will be change dynamically as users data changes from server. There are following way to implement this problem- 1. I could use poll http request in every 2 minutes to fetch data from server. There are two ways of polling First , Use Timertask class . Second Create service class and use Alarm manager. which method is best performance wise???? Later i found out that polling is real battery killer. Is there any method other than polling for realtime userlist solution ?? Which techniques modern apps like facebook uses for changing data realtime in view?

Upvotes: 1

Views: 170

Answers (4)

Gerardo Suarez
Gerardo Suarez

Reputation: 362

Hey that is an excellent question and if you are thinking about a real-time app that scales I think you have some options to do that, but for implementing some of those you will have to change the implementation of your server:

Short HTTP-polling The client sends lots of frequent requests to the server. This is the approach you are following right now.

Long-polling A single request is sent to the server and the client is waiting for the response.

Web Sockets: bi-directional communication protocol. This is a good article to think about this: https://medium.com/walmartglobaltech/exploring-websocket-and-its-brief-implementation-for-android-cc461597e1dc This can make your client and backend implementation most complex.

Server-side events: unidirectional communication from the server

In this article you will have some common useful protocols used to build real-time applications: https://arena.im/post/most-popular-instant-messaging-chat-protocols

In this article you can learn more about real time communications: https://www.telerik.com/blogs/real-time-communication-techniques

I think the easiest option to achieve that in short-time is using Firebase as Firebase use websockets under the hood with their SDK, you can get that almost for free just learning Firebase. But in that case you will have to change your current web server or see the way your current backend can interact with Firebase.

As a follow-up I want to tell you that is a tricky feature and don't take that as an easy task. There is a lot of ways to achieve a real-time behavour and each one has is pros and cons. Please do a good research about the problem.

Upvotes: 0

Ashish Tamrakar
Ashish Tamrakar

Reputation: 119

SyncAdapter/AccountManager is useful for your usecase. The Authenticator ( which calls AccountManager behind the scene) can handle authentication for your app. The SyncAdapter can handle periodic syncs from server to local datastore. You wont have to implement it yourself using AlarmManager. For samplecode Sync Adapter

Your server will work with the SyncAdapter, without much changes. The only differences would be in your client, like for example instead of calling server's fetch data methods in AlarmManager, you would instead call them in onPerformSync() of your class which extends SyncAdapter.

Hope this helps.

Upvotes: 0

Mukesh Rana
Mukesh Rana

Reputation: 4091

Instead of using Polling at reugular intervals, you should use GCM in my opinion. Whenever there is any change in users, you just send a Push Notification to your app and then in your BroadCastReceiver just hit a web api to get the latest feeds.

Upvotes: 0

user3621165
user3621165

Reputation: 212

Why it's necessary? I won't do that like this. You could have a service, which get the data from your server and save it to the local database. When you will start the service, that is your choice! I would not download each 2 minutes or something like that. Maybe you can react on users action. So if he open the app or click on chat list or something like this.

If you really need it directly, you could use GoogleCloudMessaging, but I really think, that is a terrible idea!

Upvotes: 0

Related Questions