I have an online hotel reservation system made by PHP / MySQL. I hosted it online needs a few more changes, but it looks kinda good now.
But I want it to have offline capabilities. It can do it's work even when offline and once connected, it makes changes to my online hosted site.
Kind of like when you're posting a comment on facebook and you're offline, and it says "We will post it when you connect online". It looks like it's been saved locally and when it is connected to the Internet, it does post / change / update.
How do I do that? =)
You can make this through database synchronization
, and there are two general ways achieving this: action-based
or SQL-based
.
You can store the updates in a table like buffer
, and check whether your source code has connection with the Internet, if so, read from buffer, and make updates to the database. You should specify logic in your code. This means that the online database will try to synchronize corresponding to your specific actions in buffer
table, and it will not sync other changes that are not specified.
Or you can just synchronize the database: get a copy of your production code to your local environment, and synchronize your database to the remote end, whenever the Internet connection is available. This means that the online-database will be a direct-replication of your local database.
You can use sockets here. Sockets are used when server or client update something.
For have you ever wondered when you are offline on facebook but your mobile is connected to the internet facebook can send you notification alert.
At first download socket.io frame work for client
When user connected to the internet fire an event.
var socket=io();
socket.on("connection",function(){
// fire an event });
socket.on("disconnect",function(){
// fire another event});
While it is possible to implement something yourself as Haotian Liu explained, solutions like that are very likely to leave you with lots of corrupted data over time.
A better solution is to use an offline-first database that's designed from the ground up for this purpose or at least a robust database with well-developed offline capabilities like Firebase (https://firebase.googleblog.com/2015/05/announcing-mobile-offline-support_93.html?m=1).