tags:

views:

49

answers:

1

Hi,

I have an application which runs in ofline mode also using browsers local database. Now I want my application to sync this with my server. I want to perform the below tasks

  1. Check for a connection at regular intervals
  2. When I have a connection send data to the server
  3. Get the updated data present on the server.

What should I do to accomplish my above goals?

Is WebSockets a solution to it?

A: 

You do not need WebSockets for this. I would suggest simply using XMLHttpRequest. You do need to keep track of what local data is un-synced to the server (either by placing references to un-synced data in a special "queue" when offline or by having a separate field in the local DB saying "synced"). When connected, read unsynced data and submit by XMLHttpRequest.

hallvors
So for that I will have to make a method which runs in infinite loop and keeps on checking for connection, also I will have to write some code for queuing the items added or removed, and when the connection is there i will have to send data to server. Is there a better approach to it?
Robin
Several browsers support "online" and "offline" events. You will find code to detect support for such events here: http://yura.thinkweb2.com/isEventSupported/ That way, if events are supported you just listen for them, if not you might use a slow interval thread - for example checking every 5 minutes if there is a connection. Or simply switch seamlessly to "offline" mode if the user tries to do something that requires a connection and the connection fails, then do slow polling for online..
hallvors