# API

**Kind**: interface | **Module**: [Pub Sub](https://docs.interop.io/desktop/reference/javascript/pub%20sub/index.md) | **Access**: `io.bus`

**Source**: https://docs.interop.io/desktop/reference/javascript/pub sub/api/index.html

## Methods

### publish

Publishes the provided data on a specific topic.
An optional object can be provided to publish a message to specific peers.

```ts
(topic: string, data: object, options?: MessageOptions) => void
```

**Parameters**

- **`topic`** (`string`, required)
  Topic on which to publish the message.
- **`data`** (`object`, required)
  Data to publish.
- **`options`** (`MessageOptions`, optional)
  An optional object with message options.

**Returns**: `void`

**Example**

```ts
```javascript
io.bus.publish("prices", { RIC: "VOD.L", price: 21.2 })
```
```

### subscribe

Subscribe for receiving data published on specific topic on the message bus. The provided callback will be invoked for each received message.
An optional object can be provided to receive messages published only by specific peers.

Returns a `Promise` which resolves if the subscription is successful. The `Promise` resolves with a subscription object which can be used to unsubscribe and stop receiving messages.

```ts
(topic: string, callback: (data: object, topic: string, source: IOConnectCore.Interop.Instance) => void, options?: MessageOptions) => Promise<Subscription>
```

**Parameters**

- **`topic`** (`string`, required)
  Topic to which to subscribe.
- **`callback`** (`(data: object, topic: string, source: IOConnectCore.Interop.Instance) => void`, required)
  Function that will handle the received data.
- **`options`** (`MessageOptions`, optional)
  An optional object with message options.

**Returns**: `Promise<Subscription>`

**Example**

```ts
```javascript
io.bus.subscribe(
    "prices",
    function (data, topic, source) {
        console.log(data, topic, source);
  })
```
```

## Related types

- [Instance](https://docs.interop.io/desktop/reference/javascript/interop/instance/index.md)
- [MessageOptions](https://docs.interop.io/desktop/reference/javascript/pub%20sub/messageoptions/index.md)
- [Subscription](https://docs.interop.io/desktop/reference/javascript/pub%20sub/subscription/index.md)
