diffusion
Diffusion JavaScript client
Last updated a month ago by push_technology .
LGPL-3.0+ · Original npm · Tarball · package.json
$ cnpm install diffusion 
SYNC missed versions from official npm registry.

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

build: string
The build version of this client library

Functions

function log(level: LogLevel | keyof typeof LogLevel)
Set the level of logging used by Diffusion.

function connect(options: Options): Result<Session>
Connect to a specified Diffusion server.

function escape(s: string)
Escapes special characters in a string that is to be used within a topic property or a session filter.

function stringToRoles(s: string)
Utility method which converts a string of the format required by the $Roles session property into a mutable set of strings.

rolesToString(roles: Set<string> | string[])
Utility method which converts a set of authorization roles to the string format required by the $Roles session property.

function updateConstraints(): UpdateConstraintFactory
Returns an update constraint factory.

Namespaces

const datatypes
Access to the datatypes namespace

const locks
Access to the locks namespace

const selectors
Access to the selectors namespace

const topics
Access to the topics namespace

const topicUpdate
Access to the topicUpdate namespace

const errors
The ErrorReason enum

const clients
Access to PropertyKeys and other client control options

Quick start

A client Session maintains a connection to the server. To create a session, simply do

 diffusion.connect('diffusion.example.com');

It is also possible to connect with a map of options

diffusion.connect({
   host : 'diffusion.example.com',
   port : 8080,
   secure : false,
   principal : 'admin',
   credentials : 'password'
});

Connecting returns a Promise - this will succeed if the session could be connected, or fail if not.

diffusion.connect('diffusion.example.com').then(function(session) {
    // Connected!
}, function(error) {
    // Failed to connect :(
});

Sessions emit events to indicate their status such as when they are disconnected or closed. These events are easy to listen to:

session.on('disconnect', function(reason) {
    // Lost connection to the server!
});

session.on('close', function() {
    // Session is closed!
});

Once a session is closed, it can never be re-opened.

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.

session.addStream('topic/foo', diffusion.datatypes.json()).on('value', function(topic, specification, newValue, oldValue) {
    // Do something with the value
    var value = newValue.get();
});

It is possible to register any number of streams to a subscription's events. They will each be called when a new value is received.

Contents

The JavaScript client provides namespaces, classes, and methods that support the following capabilities:

Basics

  • Connect the JavaScript client to Diffusion or Diffusion Cloud by using the diffusion.connect method.

  • To change the security principal that the client is connected with, use the changePrincipal method.

  • The client can log out information by using the diffusion.log method.

  • 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.

Create and manage topics

Creating and managing topics is handled through the topics feature.

  • Add a topic.
    Use the add method to add a topic. You can create a topic by explicitly defining the topic type, or by providing a TopicSpecification with optional properties.

  • Handle missing topics.
    Use the addMissingTopicHandler method to register a MissingTopicHandler. This handler receives a MissingTopicNotification when a client session subscribes to a topic that does not currently exist. The notified client can then choose to create that topic if appropriate.

  • Remove topics.
    Use the remove method to remove topics. You can also mark topics to be removed automatically with the TopicSpecification property REMOVAL.

Update topics

Updating topics is handled through the topics feature.

  • Update a topic using the set method.

  • Create an update stream for creating and updating a specific topic using the createUpdateStream

  • Update topics by using the classes in the diffusion.datatypes namespace to create update values.

Manage other clients

Updating topics is handled through the clients feature.

Send messages

Sending and receiving messages is handled through the messages feature.

  • Send a message.
    Use the send method to either send a message to a specific client session or send a message to a path.

  • Receive messages sent to this client session.
    Use the setRequestStream method to receive messages sent to this client session through a RequestStream.

  • Receive messages sent to a path.
    Use the addRequestHandler method to register a RequestHandler that receives messages sent to a path.

Authenticating clients

Authenticating clients and modifying the authentication store is handled through the security feature.

The server also uses information stored in the system authentication store to authenticate connecting clients.

Updating security roles

The security feature also allows querying and updating a session's security roles.

Current Tags

  • 5.9.10                                ...           diffusion-5.9.10 (2 years ago)
  • 5.9.11                                ...           diffusion-5.9.11 (2 years ago)
  • 5.9.12                                ...           diffusion-5.9.12 (2 years ago)
  • 5.9.13                                ...           diffusion-5.9.13 (2 years ago)
  • 5.9.14                                ...           diffusion-5.9.14 (2 years ago)
  • 5.9.15                                ...           diffusion-5.9.15 (2 years ago)
  • 5.9.16                                ...           diffusion-5.9.16 (2 years ago)
  • 5.9.17                                ...           diffusion-5.9.17 (2 years ago)
  • 5.9.18                                ...           diffusion-5.9.18 (2 years ago)
  • 5.9.19                                ...           diffusion-5.9.19 (2 years ago)
  • 5.9.20                                ...           diffusion-5.9.20 (a year ago)
  • 5.9.21                                ...           diffusion-5.9.21 (10 months ago)
  • 5.9.22                                ...           diffusion-5.9.22 (7 months ago)
  • 5.9.23                                ...           diffusion-5.9.23 (6 months ago)
  • 5.9.9                                ...           diffusion-5.9.9 (3 years ago)
  • 6.0.0                                ...           diffusion-6.0.0 (2 years ago)
  • 6.0.1                                ...           diffusion-6.0.1 (2 years ago)
  • 6.0.2                                ...           diffusion-6.0.2 (2 years ago)
  • 6.0.3                                ...           diffusion-6.0.3 (2 years ago)
  • 6.0.4                                ...           diffusion-6.0.4 (2 years ago)
  • 6.0.5                                ...           diffusion-6.0.5 (a year ago)
  • 6.0.7                                ...           diffusion-6.0.7 (a year ago)
  • 6.0.8                                ...           diffusion-6.0.8 (a year ago)
  • 6.0.9                                ...           diffusion-6.0.9 (9 months ago)
  • 6.1.0                                ...           diffusion-6.1.0 (2 years ago)
  • 6.1.1                                ...           diffusion-6.1.1 (a year ago)
  • 6.1.2                                ...           diffusion-6.1.2 (a year ago)
  • 6.1.3                                ...           diffusion-6.1.3 (a year ago)
  • 6.1.4                                ...           diffusion-6.1.4 (a year ago)
  • 6.1.5                                ...           diffusion-6.1.5 (10 months ago)
  • 6.1.6                                ...           diffusion-6.1.6 (6 months ago)
  • 6.1.7                                ...           diffusion-6.1.7 (10 days ago)
  • 6.2.0                                ...           diffusion-6.2.0 (a year ago)
  • 6.2.1                                ...           diffusion-6.2.1 (10 months ago)
  • 6.2.2                                ...           diffusion-6.2.2 (9 months ago)
  • 6.2.3                                ...           diffusion-6.2.3 (9 months ago)
  • 6.2.4                                ...           diffusion-6.2.4 (7 months ago)
  • 6.2.5                                ...           diffusion-6.2.5 (6 months ago)
  • 6.2.6                                ...           diffusion-6.2.6 (6 months ago)
  • 6.2.7                                ...           diffusion-6.2.7 (4 months ago)
  • 6.2.8                                ...           diffusion-6.2.8 (25 days ago)
  • 6.2.9                                ...           diffusion-6.2.9 (3 days ago)
  • 6.3.0                                ...           diffusion-6.3.0 (6 months ago)
  • 6.3.1                                ...           diffusion-6.3.1 (5 months ago)
  • 6.3.2                                ...           diffusion-6.3.2 (2 months ago)
  • 6.3.3                                ...           diffusion-6.3.3 (a month ago)
  • 6.3.3                                ...           latest (a month ago)

94 Versions

  • 6.2.9                                ...           3 days ago
  • 6.1.7                                ...           10 days ago
  • 6.2.8                                ...           25 days ago
  • 6.3.3                                ...           a month ago
  • 6.3.2                                ...           2 months ago
  • 6.2.7                                ...           4 months ago
  • 6.3.1                                ...           5 months ago
  • 6.2.6                                ...           6 months ago
  • 6.1.6                                ...           6 months ago
  • 5.9.23                                ...           6 months ago
  • 6.2.5                                ...           6 months ago
  • 6.3.0                                ...           6 months ago
  • 6.2.4                                ...           7 months ago
  • 5.9.22                                ...           7 months ago
  • 6.0.9                                ...           9 months ago
  • 6.2.3                                ...           9 months ago
  • 6.2.2                                ...           9 months ago
  • 5.9.21                                ...           10 months ago
  • 6.1.5                                ...           10 months ago
  • 6.2.1                                ...           10 months ago
  • 6.1.4                                ...           a year ago
  • 6.2.0                                ...           a year ago
  • 6.1.3                                ...           a year ago
  • 5.9.20                                ...           a year ago
  • 6.0.8                                ...           a year ago
  • 6.1.2                                ...           a year ago
  • 6.0.7                                ...           a year ago
  • 6.1.1                                ...           a year ago
  • 6.0.5                                ...           a year ago
  • 6.1.0                                ...           2 years ago
  • 6.0.4                                ...           2 years ago
  • 5.9.19                                ...           2 years ago
  • 6.0.3                                ...           2 years ago
  • 5.9.18                                ...           2 years ago
  • 5.9.17                                ...           2 years ago
  • 5.9.16                                ...           2 years ago
  • 5.9.15                                ...           2 years ago
  • 6.0.2                                ...           2 years ago
  • 6.0.1                                ...           2 years ago
  • 6.0.0                                ...           2 years ago
  • 5.9.14                                ...           2 years ago
  • 5.9.13                                ...           2 years ago
  • 5.9.12                                ...           2 years ago
  • 5.9.11                                ...           2 years ago
  • 5.9.10                                ...           2 years ago
  • 5.9.9                                ...           3 years ago
  • 5.9.8                                ...           3 years ago
  • 5.8.11                                ...           3 years ago
  • 5.9.7                                ...           3 years ago
  • 5.9.6                                ...           3 years ago
  • 5.6.10                                ...           3 years ago
  • 5.9.5                                ...           3 years ago
  • 5.9.4                                ...           3 years ago
  • 5.8.10                                ...           3 years ago
  • 5.7.9                                ...           3 years ago
  • 5.9.3                                ...           3 years ago
  • 5.8.9                                ...           3 years ago
  • 5.8.8                                ...           3 years ago
  • 5.9.2                                ...           3 years ago
  • 5.7.8                                ...           3 years ago
  • 5.9.1                                ...           3 years ago
  • 5.8.6                                ...           3 years ago
  • 5.9.0                                ...           3 years ago
  • 5.8.5                                ...           3 years ago
  • 5.8.4                                ...           3 years ago
  • 5.8.3                                ...           3 years ago
  • 5.8.2                                ...           3 years ago
  • 5.6.9                                ...           3 years ago
  • 5.7.7                                ...           3 years ago
  • 5.8.1                                ...           3 years ago
  • 5.7.6                                ...           3 years ago
  • 5.8.0                                ...           3 years ago
  • 5.7.5                                ...           3 years ago
  • 5.6.8                                ...           3 years ago
  • 5.7.4                                ...           3 years ago
  • 5.7.3                                ...           4 years ago
  • 5.7.2                                ...           4 years ago
  • 5.7.1                                ...           4 years ago
  • 5.7.0                                ...           4 years ago
  • 5.6.7                                ...           4 years ago
  • 5.6.6                                ...           4 years ago
  • 5.6.5                                ...           4 years ago
  • 5.6.4                                ...           4 years ago
  • 5.6.1                                ...           4 years ago
  • 5.6.0                                ...           4 years ago
  • 5.5.6                                ...           4 years ago
  • 5.5.5                                ...           4 years ago
  • 5.5.3                                ...           4 years ago
  • 5.5.2                                ...           4 years ago
  • 5.5.1                                ...           4 years ago
  • 5.5.0                                ...           4 years ago
  • 5.3.0                                ...           5 years ago
  • 5.2.1                                ...           5 years ago
  • 5.2.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 131
Last Day 0
Last Week 20
Last Month 173
Dependencies (10)

Copyright 2014 - 2016 © taobao.org |