Javascript (Node.js)

Install nep-js library for node.js using npm

npm install nep-js

Publisher simple example

  1. Import nep-js library

const nep = require('nep-js');
  1. Create a new node and a publisher instance

var node = new nep.Node("publisher_js");
var pub = node.new_pub(topic, "json");
  1. Then send messages using the publish function

pub.publish({"result":0})

Subscriber simple example

  1. Import nep-js library and create a new nep node

const nep = require('nep-js');
var node = new nep.Node("subscriber_js");
  1. Create a callback, which will be executed each time a new message arrives

function callbackJSON(msg) {
    console.log(msg)
}
  1. Create a subscriber instance and define the respective callback

var sub = node.new_sub(topic, "json", callbackJSON);

Last updated