2. Getting Started
2.1. Installing RTI Connector for JavaScript
RTI Connector for JavaScript can be installed with npm:
$ npm install rticonnextdds-connector
npm uses node-gyp to locally compile some of Connector’s dependencies. node-gyp requires a Python installation and a C++ compiler. Please refer to the node-gyp documentation for more details.
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()
await 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.