How to run a function when device comes to online state


#1

I have created a app that uses sqlite db.I track the events that happened when offline.Then when the device comes to online.I want to update that events to mysql db in server.how to do that??


#2

so you need an endpoint for that on server side. So you can send an http-request with the data you want to store in the sql table.

This endpoint handles an array/list of your event-objects.


#3

Server side is php and mysql.How to send http request when device comes to online state??


#4

use the $http-service of angular.


#5

How to trigger http-request when the device state changes to online
???


#6

Like this:

document.addEventListener('online', function () {
  $http.post(URL, DATA).then(successFn, errorFn).finally($scope.$apply);
});

But keep in mind you need cordova-network-information plugin to react on online/offline events.


#7

thanks :slight_smile: