Diffusion JavaScript API 6.3.2

Diffusion JavaScript API

The Diffusion JavaScript API allows interaction with a Diffusion server from both
the browser and Node.js.

Clients use a WebSocket or HTTP connection to send and receive data, as well as perform
other functions such as adding, removing or updating topics. For a full list of capabilities
-in the JavaScript API, see the Contents section below.

Installing

Install the client using npm install diffusion --save

Pre-bundled browser artifacts are included in node_modules/diffusion/dist

To import Diffusion use either require or ES6 style syntax

require

var diffusion = require('diffusion');

ES6

import * as diffusion from'diffusion';

TypeScript

The Diffusion package includes TypeScript bindings, specified in node_modules/diffusion/typescript

The diffusion object

The documentation will frequently refer to the diffusion object. Technically,
this is the module object that is obtained when importing the Diffusion library
using one of the methods described above. The diffusion object exposes a number
of global constants and functions.

Constants

version: string;
The version of this client library in the form major.minor.patch

Subscriptions

Data in Diffusion is distributed via topics. A topic has state, which can be
updated. The state can be simple - such as a string or an integer - or more
complex - such as a JSON document. Each topic has a unique path and is addressed
through a topic selector.

The way that a session receives data is by
subscribing. Subscriptions allow the session
to select one or more topics to receive values from. A session may subscribe to
many topics, as well as subscribe to the same topic multiple times.

session.select('topic/foo')

To attach listeners for received values, a ValueStream is used. The stream that is
returned will emit value events when the value of the selected topic changes.

The client can check its connectivity and roundtrip time to the server by using the pingServer method.

Receive data from topics

Subscribe to topics.Use the session.select method to subscribe to a topic.
The updates to a topic can be interacted with by registering a ValueStream and a provided datatype to start receiving
the values of that datatype.

Fetch data from topics.Use the fetch method to make a fetch request and get a FetchStream object that you can use to receive fetched values.