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

2

6

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!

Colombia answered 20/1, 2015 at 0:15 Comment(0)
S
3

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.

Spermophile answered 20/1, 2015 at 2:51 Comment(2)
hey thanks so much for the answer. Is offline an event that is transmitted by the browser? I didn't know this. The Cordova network status plugin should help ill try it. My plan is to once the user signs in, save boolean in localStorage to not show login anymore. Then if offline, and user enters data, store information in groundDB. When online event is triggered then I will check groundDB for documents and if so update the cloud storage. Sounds simple but I feel like this is gonna be tricky...Colombia
Yes, these are browser events - standard. Should not be too tricky - good luck!Spermophile
T
9

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.

Toy answered 4/2, 2015 at 21:19 Comment(1)
Since Meteor.status() is reactive, this is the best solution!Unwilling
S
3

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.

Spermophile answered 20/1, 2015 at 2:51 Comment(2)
hey thanks so much for the answer. Is offline an event that is transmitted by the browser? I didn't know this. The Cordova network status plugin should help ill try it. My plan is to once the user signs in, save boolean in localStorage to not show login anymore. Then if offline, and user enters data, store information in groundDB. When online event is triggered then I will check groundDB for documents and if so update the cloud storage. Sounds simple but I feel like this is gonna be tricky...Colombia
Yes, these are browser events - standard. Should not be too tricky - good luck!Spermophile

© 2022 - 2024 — McMap. All rights reserved.