# @truffle/dashboard-message-bus-client

> Client library for accessing the truffle dashboard's message bus

Latest version **0.1.12** (published 2023-09-07) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @truffle/dashboard-message-bus-client
pnpm add @truffle/dashboard-message-bus-client
yarn add @truffle/dashboard-message-bus-client
bun add @truffle/dashboard-message-bus-client
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.12 |
| Published | 2023-09-07 |
| First published | 2022-06-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | ^16.20 \|\| ^18.16 \|\| >=20 |
| Dependencies | 9 |
| Unpacked size | 56.1 KB |
| Known vulnerabilities | 0 (+30 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 13915 |
| Maintainers | rizedr, joshuafernandes, cliffoo, kevinbluer, gnidan, haltman, eggplantzzz, fainashalts, cds-amal |
| Keywords | ethereum, etherscan, ipfs, solidity, verify-source, sourcify, compile |

## Links

- npm: https://www.npmjs.com/package/@truffle/dashboard-message-bus-client
- Repository: https://github.com/trufflesuite/truffle
- Homepage: https://github.com/trufflesuite/truffle/tree/master/packages/dashboard-message-bus-client#readme
- Issues: https://github.com/trufflesuite/truffle/issues
- npm.io page: https://npm.io/package/@truffle/dashboard-message-bus-client

## Dependencies (9)

- [ws](https://npm.io/package/ws.md) ^7.2.0
- [axios](https://npm.io/package/axios.md) 1.5.0
- [debug](https://npm.io/package/debug.md) ^4.3.1
- [delay](https://npm.io/package/delay.md) ^5.0.0
- [isomorphic-ws](https://npm.io/package/isomorphic-ws.md) ^4.0.1
- [tiny-typed-emitter](https://npm.io/package/tiny-typed-emitter.md) ^2.1.0
- [node-abort-controller](https://npm.io/package/node-abort-controller.md) ^3.0.1
- [@truffle/promise-tracker](https://npm.io/package/@truffle/promise-tracker.md) ^0.1.7
- [@truffle/dashboard-message-bus-common](https://npm.io/package/@truffle/dashboard-message-bus-common.md) ^0.1.7

## Recent versions

- 0.1.12 (latest) — 2023-09-07
- 0.1.7-hardhat-error.0 (hardhat-error) — 2022-08-25
- 0.1.3-typescript-migrations.0 (typescript-migrations) — 2022-07-21
- 0.1.0-alpha.2 (signTypedData_v4) — 2022-06-24
- 0.1.11 — 2023-06-06
- 0.1.10 — 2023-01-27
- 0.1.9 — 2022-11-23
- 0.1.8 — 2022-10-26
- 0.1.7 — 2022-10-12
- 0.1.6 — 2022-08-24
- 0.1.6-hardhat-error.1 — 2022-08-22
- 0.1.6-hardhat-error.0 — 2022-08-20
- 0.1.5 — 2022-08-17
- 0.1.4 — 2022-08-10
- 0.1.3 — 2022-07-21
- … 4 more at https://npm.io/package/@truffle/dashboard-message-bus-client/versions

## README

# `@truffle/dashboard-message-bus-client`

This library is used for connecting with the message bus that powers the
Truffle dashboard.

## Usage

### Connecting to the message bus and producing a message

```ts
import { DashboardMessageBusClient } from "@truffle/dashboard-message-bus-client";

const client = new DashboardMessageBusClient({ port, host });

const message = {
  type: "helloWorld",
  id: new DateTime().getTime(),
  payload: {
    hello: "world"
  }
};

const messageLifecycle = await client.publish(message);
```

#### The lifecycle of published messages

Publishing a message creates a lifecycle for that message. The lifecycle is tracked by the publisher as an object of type `PublishedMessageLifecycle<MessageType, ResponseType>`.

Publish message lifecycles terminate in one of three ways:

1. Receipt of a corresponding response message
2. Invalidation of the message
3. Abandonment of the message

##### Waiting for a response to a message

To enable request/response messaging (e.g. proxied JSON-RPC messages), publish
lifecycles contain a promise that resolves when a response to the originally
published message is received.

```ts
const response = await publishLifecycle.response;
```

##### Abandoning messages

To allow for fire-and-forget messages (e.g. event data), simply call the
`abandon` method on the lifecycle object.

**Important**: If the `abandon` method of the publish lifecycle is not called,
the process will hang forever on exit, as it will forever be waiting for a
response that will never arive.

```ts
await messageLifecycle.abandon();
```

##### Invalidating messages (aka message cancellation)

The consumer of a published message often will need to perform some longer
running operation as a result of that message. To cancel that work, or to
communicate to the consumer that the message is no longer valid, simply call
`cancel` on the publish lifecycle.

```ts
await messageLifecycle.invalidate();
```

### Consuming and responding to messages

Messages can be consumed by calling the `subscribe` method of the client. The
`subscribe` method returns a `DashboardMessageSubscription` object. This object
emits `message` events whenever messages are received that match the
subscription's filter.

Received messages are wrapped up in a lifecycle object similar to the one used for publishing messages, however there's only a single `respond` method.

```ts
import { DashboardMessageBusClient } from "@truffle/dashboard-message-bus-client";

const client = new DashboardMessageBusClient({ port, host });

// omitting the type string in the options object will return a subscription
// for all messages
const subscription = client.subscribe({
  type: "helloWorld"
});

subscription.on("message", receivedMessageLifecycle => {
  const helloTarget = receivedMessageLifecycle.message.payload.hello;

  if (helloTarget.toLowerCase() === "world") {
    // respond by passing your response payload directly to the response
    await receivedMessageLifecycle.respond({
      someArbitraryResponse:
        "Why hello there. Pleased to make your acquaintance"
    });
  } else {
    await receivedMessageLifecycle.respond({
      error: `I'm sorry, you have the wrong number. This is world, not "${helloTarget}"`
    });
  }
});
```

---
_Source: https://npm.io/package/@truffle/dashboard-message-bus-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
