JavaScript
The JavaScript® API is provided in the file diffusion.js and can be accessed through the web or through NPM.
Use with Node.js:
Install with npm:
npm install diffusion
Include in your Node.js application:
var diffusion = require('diffusion');
https://download.diffusiondata.com/clients/6.11.2/js/diffusion-js-6.11.2.tgz
Include JavaScript in a web page:
<script src="https://download.diffusiondata.com/clients/6.11.2/js/diffusion-6.11.2.js"/>
This hosted version of the Diffusion™ JavaScript library is served with GZIP compression enabled. GZIP compression reduces the library to 20% of its uncompressed size and ensuring fast page loads.
Get the minified JavaScript :
https://download.diffusiondata.com/clients/6.11.2/js/diffusion-6.11.2.js
diffusion_directory/clients/js
The Diffusion JavaScript client library is a full featured library and as such is provided as a large download file. However, when served with GZIP compression, the size of the served file is significantly smaller than the size of the downloaded file. Ensure that you enable GZIP compression on the web server that hosts the JavaScript client library.
The minified version of the JavaScript client library is approximately 70% of the size of the unminified version.
Get the unminified JavaScript :
https://download.diffusiondata.com/clients/6.11.2/js/diffusion-unminified-6.11.2.js
diffusion_directory/clients/js
The minified version of the Diffusion JavaScript client library is created with Browserify. The minified version might not be compatible with certain JavaScript frameworks. This unminified version is provided to enable you to include Diffusion in projects using any framework.
The unminified form of JavaScript client library also gives you the option to perform minification of your whole client application and make further size savings.
Enable zlib compression
Diffusion clients use zlib to support message compression. Since Diffusion 6.1, zlib code for message decompression has been removed from the main JavaScript client library to reduce its size and separated out into browserify-zlib-0.2.0.js.
browserify-zlib-0.2.0.js is included in the clients/js directory of a Diffusion installation, or can be downloaded from JavaScript SDK downloads.
Include browserify-zlib-0.2.0.js for clients that want to make use of the client compression capability. This can be achieved at build time by using browserify to package the browserify-zlib npm module into the application library.
Clients will log out a warning at startup if browserify-zlib-0.2.0.js is not included. The client's initial connection request will set the per-message compression capability depending on whether it is included or not. This will indicate to the server whether messages should be compressed before they are sent to the client.
Modularized browser bundles
As well as the full client, modularized browser bundles are available to minimize page size.
This enables you to use the core bundle with minimal features, and additional bundles which can be loaded into the browser dynamically after the core bundle of the modular client.
There is also a diffusion-worker.js script which enables you to use web workers to share sessions across browser tabs.
The bundles are available in the node_modules/diffusion/dist directory of an installation with npm, or in the clients/js directory of an on-premise Diffusion installation.
See the JavaScript JavaScript API documentation for full details.
Use TypeScript definitions with the JavaScript client library:
If you got the JavaScript client library using NPM, the TypeScript definitions are included.
https://download.diffusiondata.com/clients/6.11.2/js/diffusion-6.11.2.d.ts
diffusion_directory/clients/js
Include the TypeScript definition file in your IDE project to use the TypeScript definitions when developing a JavaScript client for Diffusion .
Use with webpack
The JavaScript npm client supports the use of webpack and has been tested with webpack 4.16.2.
Targeting Node.js with webpack
plugins: [ new webpack.IgnorePlugin(/vertx/) ]
const webpack = require('webpack');
Capabilities
To see the full list of capabilities supported by the JavaScript API, see Feature support in the Diffusion API.
Support
For information about the browsers supported by the Diffusion JavaScript client, see Browser support.
Platform | Minimum supported versions | Supported transport protocols |
---|---|---|
JavaScript |
(TypeScript 1.8) |
WebSocket HTTP (Polling XHR) |
Resources
Using
- Promises
- The Diffusion JavaScript API uses the Promises/A+ specification.
- Regular expressions
-
The JavaScript client uses a different regular expression engine to the Diffusion server . Some regular expressions in topic selectors are evaluated on the client and others on the Diffusion server . It is possible that topic selectors that include complex or advanced regular expressions can behave differently on the client and on the Diffusion server .
For more information, see Regular expressions.
- Buffer
- The JavaScript client uses Node [`Buffer`] https://nodejs.org/api/buffer.html objects to send data to binary topics. To
allow code using the bundled client packages to send binary data, a polyfilled `Buffer`
constructor is made available through `diffusion.buffer`. The following example shows
how to create a `Buffer` in a browser
context.
``` const buf = diffusion.buffer.from('Hello World'); ```