Start publishing with JavaScript
Create a Node.js™ client that publishes data through topics on our cloud-based data distribution platform.
To complete this example, you need a Diffusion™ Cloud service 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 Diffusion Cloud Dashboard's test client to subscribe to foo/counter.
- 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 publishing.js file contains the following code:
const diffusion = require('diffusion'); diffusion.connect({ host : 'service-name.diffusion.cloud', principal : 'control-user', credentials : 'password' }).then(function(session) { console.log('Connected!'); var i = 0; // Create a JSON topic session.topics.add("foo/counter", diffusion.topics.TopicType.JSON); // Start updating the topic every second setInterval(function() { session.topicUpdate.set('foo/counter', diffusion.datatypes.json(), { count : i++ }); }, 1000); });