Data Sharing Between Apps

Pub Sub

JavaScript

Overview

By default, the Pub/Sub API is disabled. To enable it in your Glue42 enabled applications, set the bus property of the Glue42 configuration object to true when initializing the Glue42 library:

const config = {
    bus: true
};

window.glue = await Glue(config);

The Pub/Sub API is then accessible through the glue.bus object.

See our JavaScript Pub/Sub example on GitHub.

Publish

You can enable an application to publish a message on a specific topic by using the glue.bus.publish() method.

Method signature:

publish(topic: string,
    data: object,
    options ?: MessageOptions): void;

Publish to All Applications

Use this script if you want an application to publish a message to all applications that have subscribed to a specific topic:

glue.bus.publish(
    topic: "stocks",
    data: { RIC: "AAPL.O" }
));

Publish to All

Publish to Specific Applications

You can use the MessageOptions object to make an application publish a message only to specific applications that have subscribed to a topic.

The MessageOptions object has the following properties, all of which are optional.

Property Description
target An object that will be compared to the identity of the subscriber applications
routingKey A string which will be compared to the routing keys of the subscribers

Use this script to make an application publish a message to another application (or to multiple instances of it) with a specific name:

glue.bus.publish(
    topic: "stocks",
    data: { RIC: "AAPL.O" },
    options: { target: { application: "appName" } }
));

Publish Specific App

The Pub/Sub API compares the target property of the options argument with the identity of each application subscribed to the topic. It delivers the message only to subscribers with identity properties matching the respective properties of the target object.

The application can also publish messages with a specific routing key:

glue.bus.publish(
    topic: "stocks",
    data: { RIC: "AAPL.O" },
    options: { routingKey: "key1" }
));

The Pub/Sub API delivers messages with a routingKey to all subscribers with the same routing key and to the ones with no routing key.

Publish Routing

Subscribe

You can enable an application to subscribe for messages on a specific topic by using the glue.bus.subscribe() method.

Method signature:

subscribe(topic: string,
    callback: (data: object, topic: string, source: object) => void,
    options ?: MessageOptions): Promise<Subscription>;

The method glue.bus.subscribe() returns a Promise that resolves when the subscription is successful and rejects on error. The value of the resolved Promise is a subscription object that can also be used to unsubscribe from receiving messages.

The arguments passed to the callback are the published message, the topic and the identity of the publisher. The callback is invoked only for messages which are published after the subscription has succeeded.

Subscribing for Messages from Any Application

Use this script if you want an application to subscribe for messages from all applications:

glue.bus.subscribe(
    topic: "stocks",
    (data, topic, source) => console.log(data, topic, source)
));

Subscribe to All

Subscribing for Messages from Specific Applications

You can use the MessageOptions object to make an application subscribe for messages only from a specific application(s):

The MessageOptions object has the following properties, all of which are optional:

Property Description
target An object that will be compared to the identity of the publishing applications
routingKey A string which will be compared to the routing key the message was published with

Use this script to make an application subscribe for messages from another application (or to multiple instances of it) with a specific name:

glue.bus.subscribe(
    topic: "stocks",
    (data, topic, source) => console.log(data, topic, source)
    options: { target: { application: "appName" } }
));

The Pub/Sub API compares the target property of the options argument with the identity of the publisher. It invokes the callback only if all the properties of the target object match the respective properties of the identity of the publisher.

Subscribe Specific App

The application can also subscribe for messages with a specific routing key:

glue.bus.subscribe(
    topic: "stocks",
    (data, topic, source) => console.log(data, topic, source)
    options: { routingKey: "key1" }
));

The Pub/Sub API invokes the callback only for messages with a matching routing key and for the ones with no routing key.

Subscribe Routing

Reference

Reference