off-the-grid
v1.4.3
Published
Save data when there is no connection and replay back data as soon as connection established or within specified interval
Downloads
14
Readme
Off the Grid
When using 3rd party data analytics service in an offline capable app such as when using Electron, constant internet connection is required to keep sending data to the analytics service. When the internet connection drops out, we can't send data to the service, resulting in data loss.
Off the Grid is designed to handle this scenario. The basic idea is, Off the Grid will cache the data if our app is offline and will send the data if our app is online.
Getting Started
Installation
$ npm install --save off-the-grid
Usage
Example usage with analytics in Electron app:
// makeAnalyticsOfflineCapable.js
const electron = require("electron");
const analytics = require("your-favorite-analytics-library");
const OffTheGrid = require("off-the-grid");
let offTheGrid;
const { ipcMain } = electron;
ipcMain.on("prepareAnalytics", (event) => {
initializeOffTheGridAnalytics();
});
ipcMain.on('actions', (event, data) => {
offTheGrid.record(data.type, data.content);
})
function initializeOffLineAnalytics() {
offTheGrid = new OffTheGrid({
logFilePath: "pathToLogFile.log",
flushInterval: (1000 * 60 * 30),
cacheSize: 10000000, // log file size of 10 MB
checkConditionBeforeFlush: checkInternetConn(), // returns a promise
replayImmediately: true,
callback: (data) => {
// Do something with the data here
analytics.track(data);
}
});
}
API
Constructor
new OffTheGrid(opts)
Instantiates a new OffTheGrid
object, with opts
:
logFilePath
Path to log file. Make sure the parent folders are created prior.
flushInterval
Time interval specified in miliseconds to replay the cached data and give it to the callback.
cacheSize
Maximum log file size. If it exceeds this size, the log file will be deleted on the next interval when at that time the condition is not satisfied.
checkConditionBeforeFlush
Function to check current status of internet connection.
replayImmediately
Whether to replay the cached data immediately after instantiation.
callback(data) {}
Callback to be called when replaying each cached data.
Methods
record(message, body)
Record data.
message
isString
andbody
isJSON
.