Skip to content

The library implements a "communications bridge" between endpoints in a multi-node messaging network.

License

Notifications You must be signed in to change notification settings

hone-labs/comms-bridge

Repository files navigation

comms-bridge

The library implements a "communications bridge" between endpoints in a multi-node messaging network.

For instance, it can be used for messaging between the multiple processes required in a Chrome extension.

Usage

Install the library:

npm install --save @optio-labs/comms-bridge

Import the library:

import { CommsBridge } from "@optio-labs/comms-bridge";

Create an instance of the comms bridge:

const commsBridge = new CommsBridge("<identifier-for-this-endpoint>");

Add handlers to respond to incoming messages:

commsBridge.respond("<your-message-name>", payload => {

    //
    // Do something...
    //
    console.log("Message received!");
    console.log(payload);

    // Data that is returned from the handler 
    // is the reply payload received at other end of the message.
    return {
        // ... your data goes here ...
    };
});

Send a message to another endpoint:

await commsBridge.send({
    targetId: "<identifier-for-other-endpoint>",
    name: "<your-message-name>",
    payload: {
        // ... your data goes here ...
    },
});

Send a message and await a reply:

const replyPayload = await commsBridge.send(
    {
        targetId: "<identifier-for-other-endpoint>",
        name: "<your-message-name>",
        payload: {
            // ... your data goes here ...
        },
    },
    {
        awaitReply: true,
    }
);
console.log("Other endpoint replied with:");
console.log(replyPayload);

Add incoming and outgoing network transports:

commsBridge.addIncoming("", new IncomingMessageTransport());
commsBridge.addOutgoing("", new OutgoingMessageTransport()); 

IncomingMessageTransport and OutgoingMessageTransport are classes that should be implemented by you.

IncomingMessageTransport feeds incoming messages from some messaging system or network protocol into the communications bridge.

OutgoingMessageTransport sends outgoing messages from the communications bridge into some messaging system or network protocol.

Development setup

Clone the repo and install dependencies:

npm install

Build the library:

npm run build

About

The library implements a "communications bridge" between endpoints in a multi-node messaging network.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages