2. Getting Started

2.1. Installing RTI Connector for JavaScript

RTI Connector for JavaScript can be installed with npm in two ways:

You can pass the package name:

$ npm install rticonnextdds-connector

Or the GitHub repository:

$ npm install https://www.github.com/rticommunity/rticonnextdds-connector-js.git

In order to access the examples, run npm with the GitHub repository.

Connector works with Node.js versions 10.20.x 1 to 13.x.x 2. It currently doesn’t work with versions 14+ because one of its dependencies is not yet compatible with that version.

npm uses node-gyp to locally compile some of Connector’s dependencies. This requires Python 2.7 (it will not work with Python 3) and a relatively recent C++ compiler (such as gcc 4.8+).

On Windows systems, you can install the Windows Build Tools, which include both the Visual C++ compiler and Python 2.7.

For more information, see Supported Platforms.

2.2. Running the examples

The examples are in the examples/nodejs directory of the RTI Connector for JavaScript GitHub repository. The npm installation will copy the examples under <installation directory>/node_modules/rticonnextdds-connector/.

In the simple example, writer.js periodically publishes data for a Square topic, and reader.js subscribes to the topic and prints all the data samples it receives.

Run the reader as follows:

node examples/nodejs/simple/reader.js

And, in another shell, run the writer:

node examples/nodejs/simple/writer.js

This is what reader.js looks like:

const rti = require('rticonnextdds-connector')
const configFile = path.join(__dirname, '/../ShapeExample.xml')

const run = async () => {
  const connector = new rti.Connector('MyParticipantLibrary::MySubParticipant', configFile)
  const input = connector.getInput('MySubscriber::MySquareReader')
  try {
    console.log('Waiting for publications...')
    await input.waitForPublications()

    console.log('Waiting for data...')
    for (let i = 0; i < 500; i++) {
      await input.wait()
      input.take()
      for (const sample of input.samples.validDataIter) {
        // You can obtain all the fields as a JSON object
        const data = sample.getJson()
        const x = data.x
        const y = data.y
        // Or you can access each field individually
        const size = sample.getNumber('shapesize')
        const color = sample.getString('color')

        console.log('Received x: ' + x + ', y: ' + y + ', shapesize: ' + size + ', color: ' + color)
      }
    }
  } catch (err) {
    console.log('Error encountered: ' + err)
  }
  connector.close()
}

run()

And this is writer.js:

const rti = require('rticonnextdds-connector')
const configFile = path.join(__dirname, '/../ShapeExample.xml')

const run = async () => {
  const connector = new rti.Connector('MyParticipantLibrary::MyPubParticipant', configFile)
  const output = connector.getOutput('MyPublisher::MySquareWriter')
  try {
    console.log('Waiting for subscriptions...')
    await output.waitForSubscriptions()

    console.log('Writing...')
    for (let i = 0; i < 500; i++) {
      output.instance.setNumber('x', i)
      output.instance.setNumber('y', i * 2)
      output.instance.setNumber('shapesize', 30)
      output.instance.setString('color', 'BLUE')
      output.write()

      sleep.msleep(500)
    }

    console.log('Exiting...')
    // Wait for all subscriptions to receive the data before exiting
    await output.wait()
  } catch (err) {
    console.log('Error encountered: ' + err)
  }
  connector.close()
}

run()

You can run the reader and the writer in any order, and you can run multiple instances of each at the same time. You can also run any other DDS application that publishes or subscribes to the Square topic. For example, you can use RTI Shapes Demo.

Footnotes

1

Note that Connector for JavaScript is not compatible with versions of Node.js prior to v10.20.x.

2

Note that Connector for JavaScript is not compatible with Node.js v12.19.0 due to a regression that was introduced in that version of Node.js. Connector for JavaScript works with Node.js versions 12.18.x and 12.20.x.