Socket
Socket
Sign inDemoInstall

masq-client

Package Overview
Dependencies
1
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    masq-client

Client library for Masq


Version published
Maintainers
1
Install size
158 kB
Created

Readme

Source

Masq Client

Masq Logo

Promise-based client library for Qwant Masq. It allows applications to connect to the Masq Hub (central data manager) to store and retrieve application data.

Install

Developer

git clone https://github.com/QwantResearch/masq-client.git
cd client
npm install
npm start

Example usage

Add the client JS reference in your page:

<script type="text/javascript" src="dist/masq.js"></script>

Or the minified version:

<script type="text/javascript" src="dist/masq.min.js"></script>

You can also use the online version hosted on Github pages:

<script type="text/javascript" src="https://qwantresearch.github.io/masq-client/dist/masq.min.js"></script>

Using the client library in your app:

// Define the hub URL (where the data will be persisted)
var hubURL = 'https://qwantresearch.github.io/masq-hub/'

// Initialize the store
var masqStore = new MasqClient()

// Your app data (store)
var appData = {}

// Load all remote app state on initial connect
var getRemoteData = function () {
  return masqStore.getAll()
}
masqStore.onConnect().then(getRemoteData).then(function (res) {
  // update your local store
  appData = res
  console.log('Loaded remote app state:', appData)

  // Add some data in case it's the first time
  if (!appData['counter']) {
    appData['counter'] = 0
  }

  // Update a few values
  appData['counter']++
  appData['date'] = Date.now()

  // persist all local data remotely
  masqStore.setAll(appData).then(function () {
    console.log('Wrote updated value for key "counter":', appData['counter'])

    // retrieve the remote state for one particular key (e.g. counter)
    masqStore.get('counter').then(function (res) {
      console.log('Got remote value for key "counter":', res)

      if (appData['counter'] === 2) {
        // delete the counter data remotely
        masqStore.del('counter').then(getRemoteData).then(function (res) {
          console.log('Loaded remote app state:', res)
          // clear remote store
          masqStore.clear().then(getRemoteData).then(function (res) {
            console.log('Loaded remote app state:', res)
          })
        })
      }
    })
  })
}).catch(function (err) {
  console.log(err)
})

API

Initializing the client

var masqStore = new MasqClient()

Storing a local data object remotely

var appData = {}
appData.date = Date.now() // 1510847132596
appData.text = 'Hello'

masqStore.setAll(appData).then(function () {
  // success
}).catch(function (err) {
  console.log(err)
})

Getting all the remote data

masqStore.onConnect().then(function () {
  masqStore.getAll().then(function (data) {
    console.log(data) // prints { date: 1510847132596, text: "Hello" }
  }).catch(function (err) {
    console.log(err)
  })
}).catch(function (err) {
  console.log(err)
})

Update (set) a specific key/value pair

masqStore.set('text', 'Hello world').then(function () {
  // success
}).catch(function (err) {
  console.log(err)
})

Get the value for a specific key

masqStore.get('text').then(function (res) {
  console.log(res) // prints "Hello world"
}).catch(function (err) {
  console.log(err)
})

Delete a specific key

masqStore.del('date').then(function () {
  // success, we have deleted the key "date"
  // let's fetch all the remote data to take a look at what's left
  masqStore.getAll().then(function (data) {
    console.log(data) // prints { text: "Hello world" }
  }).catch(function (err) {
    console.log(err)
  })
}).catch(function (err) {
  console.log(err)
})

Delete (clear) all the remote data

masqStore.clear().then(function() {
  masqStore.getAll().then(function (data) {
    console.log(data) // prints {}
  }).catch(function (err) {
    console.log(err)
  })
}).catch(function (err) {
  console.log(err)
})

License

Apache-2.0

FAQs

Last updated on 19 Dec 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc