“Hello World” with Diffusion Cloud and the JavaScript SDK

Push Technology - August 14, 2020

This tutorial walks you through a very brief “Hello World” project that will show you how to send and receive data from a Diffusion Cloud service using the Diffusion JavaScript SDK.

You can watch a video version of this tutorial on YouTube.


Configure the Cloud service

If you haven’t done so already, sign up for Diffusion Cloud.

Create a 6.5 Cloud service.

For this tutorial, you will configure the service to accept anonymous connections – that is, clients do not need to provide a password to connect to your service.

In the Dashboard for your service, click Security in the left-hand panel.

Under Anonymous Sessions, click the wrench icon.

Make sure ALLOW is selected, then click SUBMIT.

Your service now allows anonymous sessions to connect with the preconfigured CLIENT role. The role grants limited permissions, so an anonymous session can read topics, but not change them.

Use the producer to publish topics

You can clone the full code for this tutorial from this Github repo.

git clone https://github.com/pushtechnology/diffusion-cloud-hello-world

In your installation directory, use npm to install dependencies:
npm install

Now let’s look at producer.js. This publishes topics to your Diffusion Cloud service. It is written with the Diffusion JavaScript SDK.

#!/usr/bin/env node
const diffusion = require('diffusion');

const args = process.argv.slice(2);
if (args.length < 5) { 
  console.error(`wrong # args, try ${process.argv[1]} host principal credentials topic-path topic-value`); 
const [host, principal, credentials, topicPath, topicValue] = args;

}).then(session => {
  const stringType = diffusion.datatypes.string();
  const stringTopicSpec = new diffusion.topics.TopicSpecification(diffusion.topics.TopicType.STRING);

return session.topicUpdate.set(topicPath, stringType, topicValue, {specification: stringTopicSpec})
}).then(() => {
}).catch(error => {
console.error(`Cannot set ${topicPath} on ${host}: ${error}`);

The producer establishes a Diffusion session, and then publishes a value to a topic path of your choice.

Run the producer with npm, replacing <HOSTNAME> <PRINCIPAL> and <PASSWORD> with the values for your service:

npm run pub -- <HOSTNAME> <PRINCIPAL> <PASSWORD> my/topic 'Hello world'

For example:

npm run pub -- youcorp-service.eu.diffusion.cloud admin password my/topic 'Hello world'

To find the <HOSTNAME>, see the Overview tab of the Dashboard (under Host:).

Find the hostname of your service on the Overview tab of the Dashboard

For <PRINCIPAL> and <PASSWORD>, you can use the admin principal you created when first setting up your service. You can also check the available system principals under the Security tab of the Dashboard. Use a principal with the ADMIN or TOPIC_CONTROL role.

You can verify that your topic was created correctly by going to CONSOLE > Topics in your Cloud service.

Try running the producer with a different value for the same topic:

npm run pub -- <HOSTNAME> <PRINCIPAL> <PASSWORD> my/topic 'Hola mundo'

See how the value in your Cloud service updates instantly.

Use the consumer to subscribe to topics

The consumer will connect anonymously and subscribe to topics. It is embedded in the web page at public/index.html.

Edit line 18 of public/index.html to use the hostname of your Diffusion Cloud service.

Now let’s look at consumer.js:

const open = require('open');
const express = require('express');
const app = express();

app.use('/node_modules', express.static('node_modules'));

const listener = app.listen(0, () => {
    const URL = `http://localhost:${listener.address().port}`;
    open(URL).then(() => {
        console.log(`Opened browser for ${URL}`);
    }).catch(error => {
        console.log(`Cannot open ${URL}: ${error}`);

Run the consumer with:

npm run sub

The browser opens and displays your topic.

Try changing the value with the producer to see the change reflected instantly on the page.

Open a few more browser windows to see that topic updates can be delivered to multiple clients at the same time. Diffusion can handle tens of thousands of subscribers.

Here’s some ideas for further experiments…

  • Log into the Diffusion Cloud Console as the admin user and look at the topic you created, change its value, observe the change delivered to the consumer.
  • Start more consumers – see how topic value updates are delivered to them all. If JavaScript isn’t your thing, write a consumer using one of our other SDKs.
  • Start up more producers, delivering data to more topics.

The Diffusion® Intelligent Event-Data Platform makes it easy to consume, enrich and deliver event-data in real-time across all network conditions. Push Technology pioneered and is the sole provider of real-time delta data streaming™ technology that powers mission-critical business applications worldwide. Leading brands use Push Technology to bring innovative products to market faster by reducing the software development efforts using the low-code features. The Diffusion® Intelligent Event-Data Platform is available on-premise, in-the-cloud, or in a hybrid configuration. Learn how Push Technology can reduce infrastructure costs, and increase speed, efficiency, and reliability, of your web, mobile, and IoT application.

Learn More: View Blog Posts and Developer Resources


Check out these other resources*


Info Center

Case Studies, White Papers, and Tech Talk notes relating to Diffusion and the customers benefiting from using it.


Quick Start Guide

Step-by-step guide to getting started fast with Diffusion or Diffusion Cloud.


Diffusion Cloud

Get the facts and the details about our Diffusion Cloud platform.