@truffle/dashboard-message-bus-client

Client library for accessing the truffle dashboard's message bus

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.
Last updated 2 years ago by gnidan .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @truffle/dashboard-message-bus-client 
SYNC missed versions from official npm registry.

@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

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.

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.

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.

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.

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}"`
    });
  }
});

Current Tags

  • 0.1.7-hardhat-error.0                                ...           hardhat-error (2 years ago)
  • 0.1.12                                ...           latest (a year ago)
  • 0.1.0-alpha.2                                ...           signTypedData_v4 (3 years ago)
  • 0.1.3-typescript-migrations.0                                ...           typescript-migrations (2 years ago)

19 Versions

  • 0.1.12 [deprecated]           ...           a year ago
  • 0.1.11 [deprecated]           ...           2 years ago
  • 0.1.10 [deprecated]           ...           2 years ago
  • 0.1.9 [deprecated]           ...           2 years ago
  • 0.1.8 [deprecated]           ...           2 years ago
  • 0.1.7 [deprecated]           ...           2 years ago
  • 0.1.7-hardhat-error.0 [deprecated]           ...           2 years ago
  • 0.1.6 [deprecated]           ...           2 years ago
  • 0.1.6-hardhat-error.1 [deprecated]           ...           2 years ago
  • 0.1.6-hardhat-error.0 [deprecated]           ...           2 years ago
  • 0.1.5 [deprecated]           ...           2 years ago
  • 0.1.4 [deprecated]           ...           2 years ago
  • 0.1.3 [deprecated]           ...           2 years ago
  • 0.1.3-typescript-migrations.0 [deprecated]           ...           2 years ago
  • 0.1.2 [deprecated]           ...           2 years ago
  • 0.1.1 [deprecated]           ...           2 years ago
  • 0.1.0-alpha.2 [deprecated]           ...           3 years ago
  • 0.1.0 [deprecated]           ...           3 years ago
  • 0.1.0-alpha.0 [deprecated]           ...           3 years ago
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (9)
Dev Dependencies (3)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE