# JavaScript

Source: https://docs.interop.io/desktop/capabilities/data-sharing/pub-sub/javascript/index.html

## Overview

The Pub/Sub API is accessible via the [`io.bus`](https://docs.interop.io/desktop/reference/javascript/pub%20sub/api/index.md) object.

> ℹ️ *See the JavaScript [Pub/Sub example](https://github.com/InteropIO/js-examples/tree/master/pub-sub) on GitHub.*

## Enabling Pub/Sub

By default, the Pub/Sub API is disabled. To enable it in your interop-enabled apps, set the `bus` property of the io.Connect configuration object to `true` when initializing the io.Connect library:

```javascript
import IODesktop from "@interopio/desktop";

const config = { bus: true };

const io = await IODesktop(config);
```

## Publish

### To All Apps

To publish a message on a specific topic to all subscribed apps, use the [`publish()`](https://docs.interop.io/desktop/reference/javascript/pub%20sub/api/index.md#API-publish) method. It accepts the message topic and the data to publish as arguments:

```javascript
const topic = "stocks";
const data = { RIC: "AAPL.O" };

io.bus.publish(topic, data);
```

![](https://docs.interop.io/desktop/images/pub-sub/pub-sub-all.mp4)

### To Specific Apps

Use the [`MessageOptions`](https://docs.interop.io/desktop/reference/javascript/pub%20sub/messageoptions/index.md) object to make an app publish a message only to specific apps that have subscribed to a topic.

The [`MessageOptions`](https://docs.interop.io/desktop/reference/javascript/pub%20sub/messageoptions/index.md) object has the following properties:

| Property | Type | Description |
|----------|------|-------------|
| `routingKey` | `string` | String which will be compared to the routing keys of the subscribers. |
| `target` | `object` | Object that will be compared to the identity of the subscriber apps. |

The following example demonstrates how to publish a message to another app (or to multiple instances of it) with a specific name:

```javascript
const topic = "stocks";
const data = { RIC: "AAPL.O" };
const options = {
    target: {
        application: "client-portfolio"
    }
};

io.bus.publish(topic, data, options);
```

![](https://docs.interop.io/desktop/images/pub-sub/pub-sub-specific-app.mp4)

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

The following example demonstrates how to publish messages with a specific routing key:

```javascript
const topic = "stocks";
const data = { RIC: "AAPL.O" };
const options = {
    target: {
        routingKey: "portfolio"
    }
};

io.bus.publish(topic, data, options);
```

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.

![](https://docs.interop.io/desktop/images/pub-sub/pub-sub-routing.mp4)

## Subscribe

### Messages from Any App

To subscribe for messages on a specific topic, use the [`subscribe()`](https://docs.interop.io/desktop/reference/javascript/pub%20sub/api/index.md#API-subscribe) method. Upon successful subscription, it resolves with a [`Subscription`](https://docs.interop.io/desktop/reference/javascript/pub%20sub/subscription/index.md) object. Use the `unsubscribe` property of this object to stop receiving messages on that topic.

Provide the topic on which you want to receive messages and a callback to handle the messages:

```javascript
const topic = "stocks";
const handler = (data, topic, source) => console.log(data, topic, source);

const { unsubscribe } = await io.bus.subscribe(topic, handler);
```

![](https://docs.interop.io/desktop/images/pub-sub/pub-sub-all.mp4)

### Messages from Specific Apps

Use the [`MessageOptions`](https://docs.interop.io/desktop/reference/javascript/pub%20sub/messageoptions/index.md) object to make an app subscribe for messages only from specific apps:

```javascript
const topic = "stocks";
const handler = (data, topic, source) => console.log(data, topic, source);
const options = {
    target: {
        application: "instrument-list"
    }
};

const { unsubscribe } = await io.bus.subscribe(topic, handler, options);
```

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.

![](https://docs.interop.io/desktop/images/pub-sub/pub-sub-specific-app.mp4)

The following example demonstrates how to subscribe for messages with a specific routing key:

```javascript
const topic = "stocks";
const handler = (data, topic, source) => console.log(data, topic, source);
const options = {
    target: {
        routingKey: "portfolio"
    }
};

const { unsubscribe } = await io.bus.subscribe(topic, handler, options);
```

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

![](https://docs.interop.io/desktop/images/pub-sub/pub-sub-routing.mp4)

## API Reference

For a complete list of the available Pub/Sub API methods and properties, see the [Pub/Sub API reference documentation](https://docs.interop.io/desktop/reference/javascript/pub%20sub/api/index.md).
