Start publishing with JavaScript
Create a Node.js™ client that publishes data through topics on the Diffusion™ server.
To complete this example, you need a Diffusion server and a development system with Node.js and npm installed on it.
You also require either a named user that has a role with the "ADMINISTRATOR" role. For more information about roles and permissions, see Role-based authorization.
and permissions. For example, theThe publisher updates the value of the foo/counter topic every second. You can watch the topic value being updated by
subscribing to the topic.
- You can use the example subscribing client from Start subscribing with JavaScript to subscribe to foo/counter and output the value on a web page.
The completed publisher.js file contains the following code:
var diffusion = require('diffusion');
diffusion.connect({
host : 'hostname',
principal : 'control-user',
credentials : 'password'
}).then(function(session) {
console.log('Connected!');
var count = 0;
// Create a topic with a default value of 0.
session.topics.add('foo/counter', count);
// Start updating the topic every second
setInterval(function() {
session.topics.update('foo/counter', ++count);
}, 1000);
});