Irrelon / ForerunnerDB

A JavaScript database with mongo-like query language, data-binding support, runs in browsers and hybrid mobile apps as a client-side DB or on the server via Node.js!
http://www.irrelon.com
721 stars 72 forks source link

Sync loaded collection between tabs #149

Open no-more opened 8 years ago

no-more commented 8 years ago

Hi,

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

Thanks.

Irrelon commented 8 years ago

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).

no-more commented 8 years ago

Hi,

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

Irrelon commented 8 years ago

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!

no-more commented 8 years ago

This seems great !

no-more commented 8 years ago

Should be optionally activated to reduce overhead maybe.

Irrelon commented 8 years ago

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.