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



    microgear-nodejs is a client library for Node.js. The library is used to connect application code or hardware with the NETPIE Platform's service for developing IoT applications. For more details on the NETPIE Platform, please visit .

    Outgoing Network Port

    Make sure ther following ports are allowed to connect from your network.

    • Non-TLS mode : 8080 and 1883 (microgear-nodejs uses this mode by default)
    • TLS mode : 8081 and 8883


    npm install microgear

    Usage example

    var MicroGear = require('microgear');
    const APPID  = <APPID>;
    const KEY    = <APPKEY>;
    const SECRET = <APPSECRET>;
    var microgear = MicroGear.create({
        key : KEY,
        secret : SECRET
    microgear.on('connected', function() {
        setInterval(function() {
  'mygear', 'Hello world.');
    microgear.on('message', function(topic,body) {
        console.log('incoming : '+topic+' : '+body);
    microgear.on('closed', function() {

    Library Usage

    Microgear.create (gearkey, gearsecret, scope)


    • config is a json object with the following attributes:
      • gearkey string - is used as a microgear identity.
      • gearsecret string comes in a pair with gearkey. The secret is used for authentication and integrity.
      • alias string - specifies the device alias.
    var microgear = MicroGear.create({
        key : "sXfqDcXHzbFXiLk",
        secret : "DNonzg2ivwS8ceksykGntrfQjxbL98",
        alias : "myplant"


    void microgear.connect (appid, callback)


    • appid string - a group of application that microgear will connect to.

    void microgear.setAlias (gearalias) microgear can set its own alias, which to be used for others make a function call chat(). The alias will appear on the key management portal of .


    • alias string - name of this microgear.

    void (gearname, message)


    • gearname string - name of microgear to which to send a message.
    • message string - message to be sent."valve","I need water");

    void microgear.publish (topic, message, [retained]) In the case that the microgear want to send a message to an unspecified receiver, the developer can use the function publish to the desired topic, which all the microgears that subscribe such topic will receive a message.


    • topic string - name of topic to be send a message to.
    • message string - message to be sent.
    • retained boolean - retain a message or not (the default is false)

    void microgear.subscribe (topic) microgear may be interested in some topic. The developer can use the function subscribe() to subscribe a message belong to such topic. If the topic used to retain a message, the microgear will receive a message everytime it subscribes that topic.


    • topic string - name of the topic to send a message to.

    void microgear.unsubscribe (topic) cancel subscription


    • topic string - name of the topic to send a message to.

    void microgear.setCachePath (path) By default, a microgear token cache file is stored in the same directory as the application within a file name of this format : 'microgear-.cache'. This function is for setting a path of microgear token cache file. It will be useful when you want to run multiple microgears of the same device key on the same location.


    • path string - file path

    void microgear.resetToken (callback) send a revoke token control message to NETPIE and delete the token from cache. As a result, the microgear will need to request a new token for the next connection.


    • callback function - this function will be called when the token reset is finished.

    Since the function resetToken() is asynchronous, to connect applicatin after token reset, the code should be as follows.


    void microgear.useTLS (tlsmode) Enable or disable TLS. For microgear-nodejs, TLS is disabled by default.


    • tlsmode boolean - The default is true (use TLS).


    An application that runs on a microgear is an event-driven type, which responses to various events with the callback function in a form of event function call:

    void microgear.on (event, callback)


    • event string - name of an event
    • callback function - callback function

    NETPIE consists of the following events:

    Event: 'connected' This event is created when the microgear library successfully connects to the NETPIE platform.

    microgear.on("connected", function() {

    Event: 'closed' This event is created when the microgear library disconnects the NETPIE platform.

    microgear.on("closed", function() {

    Event: 'error' This event is created when an error occurs within a microgear.

    microgear.on("error", function(err) {
        console.log("Error: "+err);

    Event: 'warning' This event is created when some event occurs, and a warning message will be notified.

    microgear.on("warning", function(msg) {
        console.log("Connection rejected: "+msg);

    Event: 'info' This event is created when there is some event occurs within a microgear

    microgear.on("info", function(msg) {
        console.log("Connection rejected: "+msg);

    Event: 'message' When there is an incomming message, this event is created with the related information to be sent via the callback function.

    microgear.on("message", function(topic,msg) {
        console.log("Incoming message: "+mesage);

    Event: 'present' This event is created when there is a microgear under the same appid appears online to connect to NETPIE.

    microgear.on("present", function(event) {
        console.log("New friend found: "+event.gearkey);

    Event: 'absent' This event is created when the microgear under the same appid appears offline.

    microgear.on("absent", function(event) {
        console.log("Friend lost: "+event.gearkey);


    npm i microgear-react

    Downloadslast 7 days







    last publish


    • avatar