Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

A node.js client library for RPC

PKGeorgievPKGeorgiev Sofia
edited October 11 in Mongoose OS

Hi everyone,

I've created a nodejs library for calling functions via RPC (http/ws/mqtt): https://github.com/PKGeorgiev/mongoose-os-rpc
Here's how to use:

const Rpc = require("mongoose-os-rpc").Rpc;

// Instantiate Rpc object using the short format (specifying address property)
// The Rpc object will internally create an instance of an appropriate transport
let rpc = new Rpc({
  address: 'http://deviceIpOrName/rpc'
  // OR: address: 'ws://deviceIpOrName/rpc'
  // OR: address: 'mqtt://deviceIpOrName/esp8266_C6D764'  <-- In this case esp8266_C6D764 is device's name as configured in Mongoose OS
});

// Subscribe for error events
rpc.on('error', function(){
  console.log("RPC ERROR");
  console.log(arguments);
})

// Subscribe for disconnect events
rpc.on('close', function() {
  console.log("RPC channel was closed!");
});

// Subscribe for connect events
// To distinguish between connect and reconnect use isReconnect() function
rpc.on('open', function() {
  console.log("RPC channel was opened!");

    if (!rpc.isReconnect()) {

      rpc.call("FS.List", {}, "", function(err, result, tag){

        console.log("CALLBACK (FS.List)");
        console.log(err);
        console.log(result);

        if (err) {
          // Oops, something went wrong
        } else {
          console.log(result)
        }

      });

    } else {
      console.log("The underlying transport had reconnected");
    }

});

It is still Beta and will be published to npm registry soon. Be welcome to test, report bugs or make suggestions :)

Sign In or Register to comment.