0

So I am currently trying to make some sort of a messaging app, really basic (just to learn). And I'm stuck to the point where the app has to listen to any changes on a MySQL database (that's where the app send the message) so it can update its recyclerview and put the data in it.

I have already done the part where you send the data to do the DB and to retrieve (using Volley) it but I just made a loop that repeats itself every 5 seconds to see if anything has been added to the DB, which is working but bad :/

I don't have some code to show you since I haven't done anything on this part. Ask me if you need more details :)

So I'd like my app to just keep itself updated with the database.

Thank you for taking the time to help me.

Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
Raphael
  • 3
  • 3

2 Answers2

1

This is a very broad question. If I understand correctly. you want to check if database is updated on your server. What you are attempting at this point is termed as polling.

You have following options that you could look into:

  • Send push notification to the device when a new message is sent to your server. Upon receiving the push notification, your app would make an API call to fetch the new messages.
  • Use a WebSocket. It's a 2 way persistent connection where you can send/receive data with much lower overhead.
  • Look into some existing solutions such as Firebase, Pusher Chatkit or any other providers.

You can still use polling. To save some resources, you can use exponential backoff and limit number of calls to your server. And when a new message arrives, send a push notification.

Froyo
  • 17,947
  • 8
  • 45
  • 73
  • 1
    SInce I dont want to use Firebase (I dont really understand what it is and I dont like to use the "Google Easy Way" as I call them) i'm going to use the push notification thing. I'm a total newbie to the whole data thing with android studio and in general. Do you have any website or tutorial to suggest me to learn about those basic thing ? Thank you for your answer btw. – Raphael Jun 13 '19 at 10:23
  • @Raphael https://stackoverflow.com/questions/11151894/how-to-do-push-notification-from-server-to-android-mobile This is a similar question. – Froyo Jun 13 '19 at 10:32
  • Thanks you for your help :) – Raphael Jun 13 '19 at 10:33
0

The best way is to use a realtime database instead of mysql.

I strongly suggest you to use Firebase realtime database in your chat application. If you use realtime database, you don't have to worry about database syncing(Loop that repeat itself every 5 seconds to see if anything has been added to the DB is a very bad solution).

Please have look at this simple chat app example.

  • 1
    Hi thank you for your answer but should I really use FIrebase ? I dont like those kinda sutff that google make to "Make things easier". With these we dont do the whole thing and I'm not sure to really understand what i'm doing by using their tools (Firebase, GCM ...). If you have anything else that could help me please please do so . – Raphael Jun 13 '19 at 10:32
  • I like this suggestion. I don't like the idea of using big tech for everything. It causes us to become dependent on companies that don't know what privacy means. – DevOpsSauce Nov 26 '20 at 23:51