ForerunnerDB icon indicating copy to clipboard operation
ForerunnerDB copied to clipboard

Sync loaded collection between tabs

Open no-more opened this issue 8 years ago • 6 comments

Hi,

I there a way to sync a collection between 2 opened tabs ?

Thanks.

no-more avatar Sep 09 '16 09:09 no-more

Ooo that is an interesting idea!

It IS possible since you can do inter-page messaging as part of HTML5. There are various ways we could solve this:

  1. Slow - dirty but easy: Create an interval that checks for changes to persistent storage and loads them when it does

  2. Instant - clean - slightly difficult: Create a server that can handle websockets and pass sync data around

  3. Instant - clean - easy: Pass messages between tabs using HTML5 APIs. This seems the best immediate solution but would only sync on the same domain (which I'm guessing would be just fine).

Irrelon avatar Sep 09 '16 10:09 Irrelon

Hi,

Yes I was thinking about the same options. Options 3 would be great but I don't know much about this api.

no-more avatar Sep 09 '16 11:09 no-more

https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

To make sync an automatic thing we should probably send a specific ping message to every window instance available on the window.frames array and then if that responds then we know ForerunnerDB is running on that page. Another important thing will be identifying the database and collections are the same before sending / receiving data updates to / from other windows.

So the code would follow this process:

  1. On ForerunnerDB database instantiation send a ping to all other windows
  2. If ping is responded to, add response window to a windowsToSyncWith array
  3. If we receive a ping and it is the same database, send a response and add the pinging window to the windowsToSyncWith array
  4. On any data changes, send a message to all windows in the windowsToSyncWith array about the change
  5. At regular intervals, ping the windows on the windowsToSyncWith with a message asking if they are still there - or can we hook a close event? If no response is received, remove the window from the array
  6. Hook the window.onbeforeclose event and send a message to other windows that we are closing so it will be easy to maintain a correct list of windows

This should be all we need to have full database sync between windows / tabs. It also looks like this can happen cross-origin!

Irrelon avatar Sep 09 '16 11:09 Irrelon

This seems great !

no-more avatar Sep 09 '16 12:09 no-more

Should be optionally activated to reduce overhead maybe.

no-more avatar Sep 09 '16 12:09 no-more

Yes definitely should be something that the developer must include and activate.

I think it should be a standalone plugin that is included via a script tag after including ForerunnerDB (just like the angular plugin).

Each database or individual collection should be allowed to be switched on or off so you can choose to sync an entire DB or only 1 collection if you want.

Irrelon avatar Sep 09 '16 13:09 Irrelon