Class: Subscription

Subscription

new Subscription()

Deprecated

As of 6.0, this class is deprecated. Use Subscription#asType instead. In future versions, Session#stream will return a ValueStream.

Provides an untyped stream of topic events.

Properties:
Name Type Description
selector String The selector this subscription was created for
Deprecated:
  • Yes
Fires:

Extends

Methods

asType(datatype) → {ValueStream}

Produce a ValueStream from this subscription stream, in order to receive values as a particular DataType. The ValueStream will only receive values from topics that are of matching type for the provided data type.

The lifecycle of the produced ValueStream is independent of this subscription. If this subscription was created using Session#stream without a selector (i.e as a fallback stream), the ValueStream will also be registered as a fallback stream.

Parameters:
Name Type Description
datatype DataType The data type to produce a stream for.
Returns:
A new ValueStream for the provided data type
Type
ValueStream
Example
// Produce a value stream for receiving JSON values.
var datatype = diffusion.datatypes.json();

session.stream('foo').asType(datatype).on('value', function(topic, value) {
    //...
});

close()

Close the subscription. No further events will be emitted. This operation is purely client-side; the server will still maintain any active subscriptions for this client.

off(event, listener) → {Stream}

Remove a listener from a specified event.
Parameters:
Name Type Argument Description
event String The event to remove listeners from
listener function <optional>
The listener to remove. All listeners for the event are removed if this is not specified
Inherited From:
Returns:
This stream.
Type
Stream
Examples
// Bind a single listener to the 'foo' event and then deregister it
var listener = function() {};
stream.on('foo', listener);
stream.off('foo', listener);
// Bind a listener to the 'foo' event and deregister all listeners
var listener = function() {};
stream.on('foo', listener);
stream.off('foo');

on(events, listener) → {Stream}

Register listeners against events.

A single listener may be bound to an event by passing the event name and listener function.

Multiple listeners may be bound by passing in an object, mapping event names to listener functions.

Parameters:
Name Type Argument Description
events String | Object The event name or object mapping event names to listeners
listener function <optional>
The listener to bind to the event, if passed as string.
Inherited From:
Returns:
This stream.
Type
Stream
Examples
// Bind a single listener to the 'foo' event
stream.on('foo', function(arg1, arg2) {
    console.log("Called for 'foo' event", arg1, arg2);
});
// Bind multiple listeners
stream.on({
    foo : function() { ... },
    bar : function() { ... },
    baz : function() { ... }
});

transform(transformer) → {Subscription}

Deprecated

As of 6.0, this is deprecated. Use Subscription#asType instead.

Create a new Subscription instance that is bound with a transformation function.

Parameters:
Name Type Description
transformer function The transformation function to apply to updates
Returns:
The new subscription bound with the transformation
Type
Subscription

Events

close

Emitted when the subscription has been closed using Subscription#close.

error

Emitted when the subscription request fails. No further events will be emitted after this.
Properties:
Name Type Description
error ErrorReason the error the subscription request failed with

open

Emitted when the subscription is initially opened, passing a reference to the subscription itself. This will only be fired once.

subscribe

Emitted when a topic that is selected by this Subscription's topic selector is subscribed to by this session. Once subscribed, update update events may be received for this topic
Properties:
Name Type Description
details TopicDetails the topic details of the topic to which the subscription applies
topic String the topic to which the subscription applies

unsubscribe

Emitted when a topic that was previously subscribed, has been unsubscribed. No further update events will be received from this topic until subscribed again. Unsubscriptions may occur due to the topic being removed, or through calling Session#unsubscribe - an object containing the reason is provided.
Properties:
Name Type Description
reason String the reason the unsubscription occurred
topic String the topic to which the unsubscription applies

update

Emitted when an update has been received for a topic's value. By default, values are provided as Buffer objects, unless Subscription#transform has been used to convert values to a specific type.
Properties:
Name Type Description
value Buffer the new value of the topic
topic String the topic to which the update applies