@design.estate/dees-comms
v1.0.31
Published
A communications module for enabling DOM-based messaging and synchronization across browser tabs and workers.
Maintainers
Readme
@design.estate/dees-comms
a comms module for communicating within the DOM
Issue Reporting and Security
For reporting bugs, issues, or security vulnerabilities, please visit community.foss.global/. This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a code.foss.global/ account to submit Pull Requests directly.
Install
To integrate @design.estate/dees-comms into your project, run the following command in your terminal:
pnpm add @design.estate/dees-commsThis will add the package to your project dependencies.
Usage
@design.estate/dees-comms facilitates communication within the DOM, especially between various workers and tabs in client-side JavaScript. Using TypeScript and the provided ESM syntax will enhance your development experience with better intellisense support and type safety.
Setting Up
First, make sure to import the module in your TypeScript file:
import { DeesComms } from '@design.estate/dees-comms';
interface IMyMessage {
method: 'myMethod';
request: {
someData: string;
};
response: {
reply: string;
};
}Initializing the Communication Module
Create an instance of the DeesComms class to start using the module. This instance will act as the central communication hub in your application.
const myDeesComms = new DeesComms();Sending Messages
To send messages, you'll need to create a typed request. Let's assume we are sending a message that requires a response:
// Creating a typed request
const myTypedRequest = myDeesComms.createTypedRequest<IMyMessage>('myMethod');
// Firing the request
const response = await myTypedRequest.fire({
someData: 'Hello, World!',
});
console.log('Received response:', response.reply);Receiving and Handling Messages
To handle incoming messages, you need to define handlers for the types of messages you expect to receive.
// Define the handler for the 'myMethod' message
myDeesComms.createTypedHandler<IMyMessage>('myMethod', async (request) => {
console.log('Message received:', request.someData);
// Process the incoming message...
// Return a response (if needed)
return {
reply: 'Message received loud and clear!'
};
});Communication Between Tabs or Workers
@design.estate/dees-comms uses the Broadcast Channel API and a polyfill for environments where it's not available, enabling seamless communication between different contexts like tabs, iframes, or web workers.
Complete Example
Putting it all together, let's create a small application that sends a message and listens for a response:
import { DeesComms } from '@design.estate/dees-comms';
interface IGreetRequest {
method: 'greet';
request: {
name: string;
};
response: {
reply: string;
};
}
const appDeesComms = new DeesComms();
// Setting up the listener
appDeesComms.createTypedHandler<IGreetRequest>('greet', async (request) => {
console.log(`Greetings received from ${request.name}`);
return { reply: `Hello ${request.name}, nice to meet you!` };
});
// Sending a greeting message
const greetRequest = appDeesComms.createTypedRequest<IGreetRequest>('greet');
const response = await greetRequest.fire({ name: 'Alice' });
console.log(response.reply); // Expected output: "Hello Alice, nice to meet you!"This module provides a powerful and flexible way to handle communications within the DOM, leveraging modern JavaScript features and patterns. The usage of TypeScript ensures type safety and enhances code understandability and maintainability.
License and Legal Information
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the repository license file.
Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
Company Information
Task Venture Capital GmbH Registered at District Court Bremen HRB 35230 HB, Germany
For any legal inquiries or further information, please contact us via email at [email protected].
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
