Keymetrics API Wrapper
This module lets you implement a fully customizable Keymetrics client, receiving live data from the Keymetrics API.
You will need to retrieve your Keymetrics user token to use it.
For more informations about the data structures and API calls check out the documentation.
We designed 2 ways to start the module: via callback or events.
Install
With NPM:
$ npm install keymetrics-api --save
Usage example
var Keymetrics = require('keymetrics-api');
var km = new Keymetrics({
token: '[token]',
public_key: '[public_key]',
realtime: true
});
km.init(function(err, res) {
if (err) return console.log(err);
km.bucket.fetchUserRole(function(err, res) {
console.log('Current permissions: ' + res);
});
km.bus.on('data:*:status', function(data) {
console.log(data);
});
});
Options
token
(Required): Refresh token obtained from the Keymetrics dashboard.token_type
(Optional): Token type (refresh_token
or access_token
)public_key
(Optional): When defined, lets you retrieve the correct bucket.realtime
(Optional): When defined with public_key, launches the websocket session at start.
This snippet makes 3 successive calls (if all options are set):
- POST to get and access_token from the servers
- GET to retrieve the correct bucket informations
- POST to set the Bucket active and initialize the Websocket connection.
Basic use - events
bus
is broadcasting events corresponding to every step of the authentication process.
In this example we start the authentication, then retrieve the bucket and finally start the realtime interaction.
var km = new Keymetrics({
token: '[token]'
});
km.bus.on('auth:ready', function(token) {
km.bucket.connect('[public_key]');
});
km.bus.on('bucket:active', function(id) {
km.bucket.fetchUserRole(function(err, res) {
console.log('Current permissions: ' + res);
});
km.realtime.init();
});
km.bus.on('realtime:on', function() {
console.log('Realtime started!')
});
km.init();
List of broadcasted events:
Authenticate
Bucket
Realtime
- realtime:on
- realtime:off
- realtime:reconnect
- realtime:reconnect-timeout
- realtime:auth
- raw:[server_name]:status
- data:[server_name]:status
- data:[server_name]:server_name
- data:[server_name]:monitoring
User
- user:logged_in
- user:logged_out
Error
- error:auth
- error:realtime
- error:bucket
DEBUG=*
allows verbose mode for received realtime data.
License
Apache 2.0