Just a second...

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 modify_topic and update_topic permissions. For example, the "ADMINISTRATOR" role. For more information about roles and permissions, see Role-based authorization.

This example steps through the lines of code required to publish to a topic. There are several different topic types which provide data in different formats. This example shows you how to publish to a JSON topic. The full code example is provided after the steps.
  1. Install the Diffusion JavaScript® library on your development system.
    npm install --save diffusion
    
  2. Create the JavaScript file that will be your publishing client.
    For example, publishing.js
    1. Require the Diffusion library.
      const diffusion = require('diffusion');
      
    2. Connect to Diffusion Cloud .
      diffusion.connect({
          host : 'service-name.diffusion.cloud',
          principal : 'control-user',
          credentials : 'password'
      }).then(function(session) {
          console.log('Connected!');
      });
      

      Where service-name is the name of your Diffusion Cloud service, control-user is the name of a user with the permissions required to create and update topics, and password is the user's password.

    3. Create a JSON topic called foo/counter.
      session.topics.add("foo/counter", diffusion.topics.TopicType.JSON);
      
    4. Every second update the value of the topic with the value of the counter.
      setInterval(function() {
          session.topicUpdate.set('foo/counter', diffusion.datatypes.json(), { count : i++ });
      }, 1000);
      
  3. Use Node.js to run your publishing client from the command line.
    node publishing.js
    
The 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);

});
Now that you have the outline of a publisher, you can use it to publish your own data instead of a counter.