Best way to data sync between Rails app and phone


I want build an Rails API which serves as backend for a mobile app. Since the mobile app will be used in offline mode. Hence I want data updates needs to be synced between mobile and web app. What would be the best possible solution for this kind problems?

The one that meets your requirements, which you haven't defined.

* Where is the data changing that needs to be "synced"?

* Is the synced data on the phone unique to that user or shared?

* How frequently does it change and how big are the changes?

* How critical is it if the phone goes offline with data that's 5 seconds
old? 5 minutes? An hour?

etc. etc.

Draw a diagram of your application data flow and work from there.


Data will get updated on both sides ie., admin update data on web which needs to be synced with mobile and vice versa. Most of the users use mobile application offline mode as there is no network connectivity. Once the work done for entire day, they will be online and starts sync the data with server. Then we needs to sync recent updates. The data is unique for that user and remaining the users wont update the data apart from admin.

Hope this helps. Let me know if you need more info.

So once a day when the user comes back online she or he opens
the app and it connects to the backend, sends its data and fetches
any updates. Sounds pretty straightforward.

What specific question do you have about this?

My question is there any gem available or do we need to use any service to make sync successful ?

Your client is sending requests to your web app. Other than Rails
itself (and the DB behind it) what do you imagine that you need?

Thanks for your reply. I felt that we need a separate server to stream. If it can be done just with Rails & DB then I am fine.

I achieve this by writing to an sqlite db in the android app and posting the records to the rails app when online.


There’s also a book on that.

Build iOS Database Apps with Swift and SQLite =>


You should take a look at Firebase. It does a great job on sync at mobile apps and the cloud database. Then you’ll need only to create a service to sync web database to cloud database, if needed.


In addition to the book mentioned in this thread, this site with RailsGuides is super helpful too:

The trickiest part of this type of architecture is handling problems like multiple updates to the same object from different sources. Since it sounds like you are not going to have much of that – but it does need to be handled in some way if it can happen at all!

Google’s Protobuf stuff is pretty good for packaging data in a cross language portable way and is much faster than JSON in RoR. But if your volume of data and connections is reasonable, then JSON is pretty easy to deal with in Ruby, Java and I assume Swift.

Using simple object APIs in RoR is what we have done in the past. If you don’t use some external service, you usually end up creating a simple protocol for the phones to use to communicate with the back end.

Communications from the server to the phone can be done in an easy way by having the mobile device periodically check for updates, at which time the server can tell it what messages it needs. The more complicated way is to use the platform notification system (On Android it is Google Cloud Messaging) to tell the device to check-in with the server. The important point is that these push notification systems don’t typically guarantee delivery. Google for example says it may deliver 1 message even if you send 10. So that means you really only want to use it to poke the app into checking in with the server.