Diffusion JavaScript Client

6.12.1 · active · verified Sun Apr 19

The Diffusion JavaScript Client is a comprehensive library for interacting with Diffusion servers (on-premise or Diffusion Cloud) from both browser and Node.js environments. It enables real-time data streaming, pub/sub messaging, and management of topic trees over WebSockets or HTTP. Currently stable at version 6.12.1, the library typically sees regular updates, with minor versions being backward-compatible, while major versions (e.g., v5 to v6) often introduce breaking changes that require client application updates. Key differentiators include its robust support for diverse topic types, built-in TypeScript definitions, and modular bundles for optimized loading, alongside its use of Promises for asynchronous operations. It is designed for applications requiring high-performance, secure, and scalable real-time data distribution infrastructure.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to a Diffusion server, subscribe to a JSON topic, and receive real-time updates. It also shows how to publish a message to the topic, handling connection errors and ensuring proper session closure.

import * as diffusion from 'diffusion';

async function runDiffusionClient() {
  const host = process.env.DIFFUSION_HOST ?? 'ws://localhost:8080';
  const principal = process.env.DIFFUSION_PRINCIPAL ?? 'admin';
  const credentials = process.env.DIFFUSION_CREDENTIALS ?? 'password';
  const topicPath = 'my/topic/path';

  let session: diffusion.Session | undefined;
  try {
    session = await diffusion.connect({
      host: host,
      principal: principal,
      credentials: credentials,
      // secure: true // Uncomment for WSS connections
    });
    console.log(`Connected to Diffusion server: ${session.sessionId}`);

    // Create a value stream for a JSON topic
    session.addStream(topicPath, diffusion.datatypes.json()).on(
      'value', 
      (topic, spec, newValue, oldValue) => {
        console.log(`Update for ${topic}: ${JSON.stringify(newValue?.get())}`);
      }
    );

    // Subscribe to the topic
    await session.select(topicPath);
    console.log(`Subscribed to topic: ${topicPath}`);

    // Example: Publish a JSON value (requires appropriate permissions on the Diffusion server)
    const topicControl = session.topicUpdate.createUpdateContext();
    await topicControl.set(
      topicPath,
      diffusion.datatypes.json(),
      diffusion.datatypes.json().newValue({ message: 'Hello, Diffusion!', timestamp: Date.now() })
    );
    console.log('Published a message to the topic.');

    // Keep the session open for a bit to receive updates
    await new Promise(resolve => setTimeout(resolve, 10000));

  } catch (error: any) {
    console.error('Diffusion connection or operation failed:', error.message || error);
  } finally {
    if (session) {
      await session.close();
      console.log('Diffusion session closed.');
    }
  }
}

runDiffusionClient();

view raw JSON →