Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect an offline Meteor Cordova app, and then use GroundDB to temp store data until back online?

Here is the goal:

A farmer has a smartphone and goes into his barn to take inventory with the app. Sometimes his phone does not get internet connection in the barn, so the app needs to work offline, store data offline, and then sync it to the cloud once it is reconnected.

The farmer should also have to only login and register the app once, and then each time they open the app, it should just open to the home screen and not ask for login info ever again. How exactly could this be done?

I am using Meteor to make the app, and Meteor's built in Cordova to package the app. I have seen GroundDB: https://github.com/GroundMeteor/db

As far as I can tell in the docs, I know how to create a local collection and use collection.insert to add data to that collection.

But I am lost as to how to code the app to open on the phone without needing internet, and detecting if internet is present or not?

Then how do I detect if internet has come back, and then sync all the data stored in offline mode?

If someone can help lay out how this would work that would be greatly appreciated!

like image 631
Nearpoint Avatar asked Jan 20 '15 00:01

Nearpoint


2 Answers

With Meteor you should use Meteor.status() which return a object of this form :

Object {status: "connected", connected: true, retryCount: 0}

connected will be obviously false if you lost the connection.

It's better than the jQuery or cordova approach because it will handle lost of connection with the meteor server thus also server or network failure not only the internet status.

like image 200
Edwin Joassart Avatar answered Sep 25 '22 19:09

Edwin Joassart


Use code like this to detect offline/online

jQuery(window).on('offline', function (e) {
    console.log('offline');
}).on('online', function (e) {
    console.log('online');
});

You could also combine it with this plugin https://github.com/apache/cordova-plugin-network-information/blob/master/doc/index.md to determine more information about the networks status if required.

like image 35
unobf Avatar answered Sep 22 '22 19:09

unobf