Just a second...

JavaScript

The JavaScript® API is provided in the file diffusion.js and can be accessed through the web or through NPM.

Include JavaScript in a web page:

<script src="http://download.pushtechnology.com/clients/5.9.23/js/diffusion.js"/>

Use with Node.js:

Install with NPM:

npm install --save diffusion

Include in your Node.js application:

const diffusion = require('diffusion');

Get the minified JavaScript:

Download the latest JavaScript file from the following URL:
http://download.pushtechnology.com/clients/5.9.23/js/diffusion.js
You can also download the JavaScript file as a tarball that can be installed locally by using NPM:
http://download.pushtechnology.com/clients/5.9.23/js/diffusion-js-5.9.23.tgz

Use TypeScript definitions with the JavaScript client library:

If you got the JavaScript client library using NPM, the TypeScript definitions are included.

You can also download a TypeScript definition file from the following URL:
http://download.pushtechnology.com/clients/5.9.23/js/diffusion-5.9.23.d.ts

Include the TypeScript definition file in your IDE project to use the TypeScript definitions when developing a JavaScript client for Diffusion™ Cloud.

Capabilities

To see the full list of capabilities supported by the JavaScript API, see Feature support in the Diffusion API.

Support

Table 1. Supported platforms and transport protocols for the client libraries
Platform Minimum supported versions Supported transport protocols
JavaScript

es6

(TypeScript 1.8)

WebSocket

HTTP (Polling XHR)

Resources

Using

Promises
The Diffusion Cloud JavaScript API uses the Promises/A+ specification.
Views

The JavaScript API provides a view capability.

Use views to subscribe to multiple topics by using a topic selector and receive all the data from all topics in the selector set as a single structure when any of the topics are updated. If the topic selector matches a topic which is subsequently added or removed, the view is updated.

The following example shows views being used to present data from multiple topics as a single structure:
diffusion.connect({
    host        : 'localhost',
    port        : 8080,
    secure      : false,
    principal   : 'control',
    credentials : 'password'
}).then(function(session) {

    // Assuming a topic tree:
    //
    // scores
    //   |-- football
    //   |     |-- semi1
    //   |     |-- semi2
    //   |     |-- final
    //   |
    //   |-- tennis
    //         |-- semi1
    //         |-- semi2
    //         |-- final

    // Use a regular expression to create a view of the topics tracking the
    // scores during the finals for each sport.
    var view = session.view('?scores/.*/final');

    // Alternatively, we can use a topic set. Note that the topics do not need
    // to be under a common root, they may be anywhere within the topic tree.
    var view2 = session.view('#>scores/football/final////>scores/tennis/final');

    // If any of the topics in the view change, display which topic changed
    // and its new value.
    view.on({
        update : function(value) {
            // Get and print the entire view structure.
            console.log('Update: ', JSON.stringify(value, undefined, 4));

            // Get individual topics. Returns a Buffer, which is automatically
            // converted to a String during concatenation, below.
            //
            // Note that the structure may not exist if the value has not been
            // updated.
            console.log('Football score: ' + value.scores.football.final);
            console.log('Tennis score  : ' + value.scores.tennis.final);

            // or ...
            // console.log('Football score: ' + value['scores']['football']['final']);
        }
    });

    // The structure can also be accessed outside the update event.
    console.log('Football score: ' + view.get().scores.football.final);
});
Regular expressions

The JavaScript client uses a different regular expression engine to Diffusion Cloud. Some regular expressions in topic selectors are evaluated on the client and others on Diffusion Cloud. It is possible that topic selectors that include complex or advanced regular expressions can behave differently on the client and on Diffusion Cloud.

For more information, see Regular expressions.