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 => {
console.log("Message received!");
console.log(payload);
return {
};
});
Send a message to another endpoint:
await commsBridge.send({
targetId: "<identifier-for-other-endpoint>",
name: "<your-message-name>",
payload: {
},
});
Send a message and await a reply:
const replyPayload = await commsBridge.send(
{
targetId: "<identifier-for-other-endpoint>",
name: "<your-message-name>",
payload: {
},
},
{
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