Giter Site home page Giter Site logo

beebotte / bbt_node Goto Github PK

View Code? Open in Web Editor NEW
8.0 4.0 2.0 130 KB

This Nodejs package implements the REST & Stream (pub/sub) API of Beebotte, an open cloud platform for the Internet of Things and real time connected applications.

License: MIT License

JavaScript 100.00%

bbt_node's Introduction

Beebotte Node.JS SDK

npm version

what where
overview http://beebotte.com/overview
tutorials http://beebotte.com/tutorials
apidoc http://beebotte.com/docs/restapi
source https://github.com/beebotte/bbt_node

Bugs / Feature Requests

Think you.ve found a bug? Want to see a new feature in beebotte? Please open an issue in github. Please provide as much information as possible about the issue type and how to reproduce it.

https://github.com/beebotte/bbt_node/issues

Install

To install the most recent release from npm, run:

npm install beebotte

Introduction

This Node.js library provides:

  • REST client, and,
  • Stream pub/sub client to Beebotte API

The Stream API supports two transports:

  • MQTT
  • Socket.io

Remember, Beebotte resource description uses a two levels hierarchy:

  • Channel: physical or virtual connected object (an application, an arduino, a coffee machine, etc) providing some resources
  • Resource: most elementary part of Beebotte, this is the actual data source (temperature from a domotics sensor)

Usage

REST Connector

  //Include the Beebotte SDK for nodejs
  var bbt = require('beebotte');

  //Create a Beebotte connector
  //Replace API key and secret key by those of your account
  var client = new bbt.Connector({apiKey: 'API KEY', secretKey: 'SECRET KEY'});

  //write (persistent message) to a given channel & resource
  client.write(
    {channel: 'mychannel', resource: 'resource1', data: 'Hello World'},
    function(err, res) {
      if(err) throw(err);
      console.log(res);
  });

  //publishe (transient message) to a given channel & resource
  client.write(
    {channel: 'mychannel', resource: 'resource1', data: 'Hello World'},
    function(err, res) {
      if(err) throw(err);
      console.log(res);
  });

  //read persistent messages from a given channel & resource
  client.read({
    channel: 'mychannel',
    resource: 'resource1', 
    limit: 5/* Retrieves last 5 records */
  }, function(err, res) {
    if(err) throw(err);
    console.log(res);
  });

Stream Connector - MQTT transport

  //Include the Beebotte SDK for nodejs
  var bbt = require('beebotte');

  //Replace API and secret keys by those of your account
  var transport = {
    type: 'mqtt',
    apiKey: 'API KEY', 
    secretKey: 'SECRET KEY',
  }
  
  //Create a Stream connector
  client = new bbt.Stream({ transport: transport });

  //On successful connection
  client.on('connected', function() {
    //subscribe to a channel/resource 
    client.subscribe( 'mychannel', 'myresource', function(message){
      console.log(message);
    })
    //On successful subscription
    .on('subscribed', function(sub) {
      client.publish( 'mychannel', 'myresource', 'Hello World');
    });
  });

You can use the Channel Token to authenticate the connection. Using the Channel Token grants read and write access to any resource of that channel. This is the recommended authentication sc heme.

  //Replace Channel Token by that of your account
  var transport = {
    type: 'mqtt',
    token: 'CHANNEL_TOKEN'
  }

Stream Connector - Socket.io transport

  //Include the Beebotte SDK for nodejs
  var bbt = require('beebotte');

  //Replace API and secret keys by those of your account
  var transport = {
    type: 'socketio',
    apiKey: 'API KEY',
    secretKey: 'SECRET KEY'
  }

  // Alternatively, you could specify an authentication endpoint (see beebotte.com/docs/clientauth)
  //Replace API key by that of your account
  var transport = {
    type: 'socketio',
    apiKey: 'API KEY', 
    auth_endpoint: 'YOUR AUTH ENDPOINT', //See https://beebotte.com/docs/clientauth 
  }
  
  //Create a Stream connector
  client = new bbt.Stream({ transport: transport });

  //On successful connection
  client.on('connected', function() {
    //subscribe to a channel/resource (with read and write access)
    client.subscribe( 'mychannel', 'myresource', {read: true, write: true}, function(message){
      console.log(message);
    })
    //On successful subscription
    .on('subscribed', function(sub) {
      client.publish( 'mychannel', 'myresource', 'Hello World');
    });
  });

You can use the Channel Token to authenticate the connection. Using the Channel Token grants read and write access to any resource of that channel. This is the recommended authentication scheme.

  //Replace Channel Token by that of your account
  var transport = {
    type: 'socketio',
    token: 'CHANNEL_TOKEN'
  }

Alternatively, you could specify an authentication endpoint (see client authentication)

  //Replace API key by that of your account
  var transport = {
    type: 'socketio',
    apiKey: 'API KEY',
    auth_endpoint: 'YOUR AUTH ENDPOINT', //See https://beebotte.com/docs/clientauth
  }

License

Copyright 2020 Beebotte.

The MIT License

bbt_node's People

Contributors

bachwehbi avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

thunderace iotier

bbt_node's Issues

Add support for protocol bridging

Beebotte provides protocol bridging:

Example: a publish using the REST API will be received by subscribers using Socket.io or MQTT.

Some message format normalization is required by the client connectors.

Unsubscribe from all active subscriptions

Instead of unsubscribing from every single active subscription, it would be useful to have an unsubscribeAll method.

//unsubscribes from all active subscriptions
unsubscribeAll() 

//unsubscribes from all active subscriptions on a given channel
unsubscribeAll(someChannel)

writeBulk not working

I am trying a simple test and I can't get it to work.
Firstly there is an error in the example at http://beebotte.com/tutorials/activity_reporter, the example code for writeBulk is incorrect.
Secondly, though I only have 5 resources defined in my channel. I've even tried setting them to type "any", I always get an error.

The error is: { error:
{ message: '[{"message":"data is not allowed","path":"data","type":"object.allowUnknown"}]',
code: 11 } }

The data is: { Uno1Temp: 17,
Uno1Humid: 39,
TH1Temp: 15.6,
TH1Humid: 49,
Uno1Light: 144 }

connected event not emitted on multiplexed connection

Can be reproduced with the following example

// with transport set to Socket.io
var client1 = new bbt.Stream(opts)
client1.on('connected', function() {
    console.log('Client 1 connected')
})

//Wait some time

var client2 = new bbt.Stream(opts)
client2.on('connected', function() {
    console.log('Client 2 connected')
})

Output:

Client 1 connected

Client 2 will not emit the connected event.

rename sendEvent method to publish

sendEvent method is intended to PUBLISH (transient message) data to Beebotte. For coherence reasons, the method name should be changed to publish.

Both methods can coexist for backward compatibility.

Update read limit param

Read limit is now 50000 instead of 2000.
The REST API connector needs to be updated accordingly.

Problem getting all resources of a channel

Format validation error is returned when any of these is called:

bclient.getResource({
  channel: 'channeltest',
  resource: ''
}, function(err, res) {
})

bclient.getResource({
  channel: 'channeltest',
  resource: '*'
}, function(err, res) {
})

bclient.getResource({
  channel: 'channeltest'
}, function(err, res) {
})

It should be possible to get all resources of a channel by leaving the resource parameter empty, setting it to wildcard '*' or simply not providing it.

Problem with connect after disconnect

Calling connect after disconnect will not make the socket to connect again.

When disconnectis called, the client is not usable anymore.

// with transport set to Socket.io
var client = new bbt.Stream(opts)
client.on('connected', function() {
  console.log('Client connected')
}).on('reconnected', function() {
  console.log('Client reconnected')
}).on('disconnected', function() {
  console.log('Client disconnected')
}).

//Wait some time
client.disconnect()

//Wait some time
client.connect()

Output:

Client connected
Client disconnected

Client will not emit the reconnected event.

Problem with unsubscribe

It is impossible to subscribe again to a channel after unsubscribing from it.

client.subscribe( channel, resource, callback );
client.unsubscribe( channel, resource );
client.subscribe( channel, resource, callback ); // subscription will not be made.

Add more built-in types

Add more built in types for the common used sensors.

Built in types have the advantage of simplifying the statistics gathering and have integrated data validation.

Unable to connect using Channel Token with MQTT transport

The only authentication method being activated is using API & Secret keys;
Channel Token based authentication does not work.

The problems comes from the initialization phase in the SDK and not in the connection establishment.

Make protocol routing optional

For publish or write operations, messages are by default routed to other transport.

If MQTT is used: published messages will be routed to Socket.io.

It would be good to add an option in order to let the user configure if routing should be performed.

Problem with "userinfo" using GET authentication endpoint

This is specific to Socket.io transport.

When using authentication endpoint with GET method, userinfo is not correctly sent to the remote endpoint if it is a JSON object.

userinfo = {name: "somename", nickname: "somenickname"}

In the query part of the GET request we will receive:

....&userinfo=

This seems to be associated with the use of querystring

Simplify get all channels function parameters

When requesting to get all channels the following are equivalent:

bclient.getChannel('*', function(err, res) {})

bclient.getChannel('', function(err, res) {})

bclient.getChannel(null, function(err, res) {})

It makes sense for getting all channels to have only one parameter: the callback function:

bclient.getChannel(function(err, res) {})

Fix example code

The given example doesn't seem to be working, It still uses "device" instead of "channel".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.