localsync
a lightweight module to sync JS objects in realtime across tabs / windows of a browser. Uses local storage event emitters and falls back to cookie polling if using an unsupported browser (IE 9+ / Edge). Isomorphic. Tested with mocha.


Install
npm i -S localsync
How to use
import localsync from 'localsync'
const action = (userID, first, last) => ({ userID, first, last })
const handler = (new, old, url) => {
console.info(`Another tab at url ${url} switched user from "${old.first} ${old.last}" to "${new.first} ${new.last}".`)
}
const usersync = localsync('user', action, handler)
usersync.start()
if(usersync.isFallback)
console.warn('browser doesnt support local storage synchronization, falling back to cookie synchronization.')
usersync.trigger(1, 'jimmy', 'john')
setTimeout(() => {
usersync.trigger(2, 'jane', 'wonka')
}, 5000)
setTimeout(() => {
if(usersync.isRunning)
usersync.stop()
}, 10000)