Needless Patchouli Manufacture
    Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    localstorage-scuttlebuttpublic

    localstorage-scuttlebutt

    replicate localstorage through scuttlebutt.

    The same api as scuttlebutt/model but data is saved in the browser using local storage.

    Example

    var LSSB = require('localstorage-scuttlebutt')
    var ls = new LSSB({prefix: prefix, id: id, store: localStorage})
     
    ls.set(key, value)
    ls.get(key)
    ls.on('change', function (key, value) {
     
    })

    prefix is the name of this instance. If you are using multiple instances, or have other data in localStorage you need to set this. By default, prefix='_lss'.

    id should be a unique string that represents the user session. you should save this in localStorage (or a cookie). The same id may be reused across multiple tabs, but not across different browsers or devices.

    store defaults to localStorage, but sessionStorage may also be used. (store must have length property and a key() method.

    Replication

    replicate with a normal scuttlebutt/model, across anything that provides a Stream api.

    //client
    var reconnect = require('reconnect')
    var LSSB = require('localstorage-scuttlebutt')
    var ls = new LSSB({id: ID, prefix: prefix})
     
    reconnect(function (stream) {
      stream.pipe(ls.createStream()).pipe(stream)
    }).connect('/shoe')
    //server.js
    var shoe = require('shoe')
    var Model = require('scuttlebutt/model')
    var model = new Model(SERVER_ID)
     
    shoe(function (stream) {
      stream.pipe(model.createStream()).pipe(stream)
    }).install(http.createServer(function (req, res) {
      fs.createReadStream(__dirname + '/index.html').pipe(res)
      req.resume()
    }).listen(3000))

    API

    Local

    opts represents the identity of the current machine. It should corrispond to the user session in this browser. prefix is the name of this object. You may use multiple localstorage-scuttlebutt instances in different parts of the same application by using different prefixes.

    var LSSB = require('localstorage-scuttlebutt')
    var opts = {id: id, prefix: prefix}
    var model = new LSSB(opts)

    createStream()

    connect to a remote instance via a duplex stream.

    reconnect(function (stream) {
      stream.pipe(model.createStream()).pipe(stream)
    }).connect('/shoe')
    

    set(key, value)

    set a property on an instance.

    model.set(key, value)

    get(key)

    get a property on an instance.

    model.get(key)

    keys()

    get all keys on instance as an array.

    model.keys()

    each(function (value, key) {...})

    iterate over all value, key pairs.

    model.each(function (value) {
      console.log(value)
    })

    value is first, like in [].forEach

    License

    MIT

    Keywords

    none

    install

    npm i localstorage-scuttlebutt

    Downloadsweekly downloads

    19

    version

    2.0.1

    license

    none

    repository

    githubgithub

    last publish

    collaborators

    • avatar