Giter Club home page Giter Club logo

web-worker-boss's Introduction

Web Worker Boss

Web Worker Boss is a lightweight library that provides a simple and intuitive socket-like API for sending and receiving messages between the main thread and the worker.

Installation

You can install Web Worker Boss using npm or yarn:

npm install web-worker-boss

or

yarn add web-worker-boss

Usage

To use Web Worker Boss for bi-directional communication, follow these steps:

  1. Import the Boss class from the web-worker-boss package:
import Boss from 'web-worker-boss';
  1. In the main thread, create an instance of Boss, passing in the web worker object as a parameter:
const boss = new Boss(worker);
  1. Register event listeners in the main thread using the on method:
boss.on('eventName', (payload) => {
  // Handle the received event and payload from the web worker
});
  1. Emit events to the web worker using the emit method:
boss.emit('eventName', payload);
  1. In the web worker script, import the Boss class from the web-worker-boss package. Make sure you are using webpack or Rollup to bundle the web worker script.
import Boss from 'web-worker-boss';
  1. Create an instance of Boss in the web worker, passing in the self object as a parameter:
const boss = new Boss(self);
  1. Register event listeners in the web worker using the on method:
boss.on('eventName', (payload) => {
  // Handle the received event and payload from the main thread
});
  1. Emit events back to the main thread using the emit method:
boss.emit('eventName', payload);

Example

Here's an example demonstrating the usage of Web Worker Boss for bi-directional communication:

Main script:

import Boss from 'web-worker-boss';

const worker = new Worker('worker.js');
const boss = new Boss(worker);

boss.on('answer', (payload) => {
  console.log('Received answer from web worker:', payload.data.answer);
});

boss.emit('ask', { question: 'How are you?' });

Web worker script (worker.js):

import Boss from 'web-worker-boss';

const boss = new Boss(self);

boss.emit('connection', { message: 'Hello, boss!' });

boss.on('ask', (payload) => {
  if (payload.data.question === 'How are you?') {
    boss.emit('answer', { answer: 'I am fine!' });
  } else {
    boss.emit('answer', { answer: 'I don\'t understand' });
  }
});

In this example, the main script creates a web worker using the Worker constructor and creates an instance of Boss with the worker. It registers an event listener for the 'answer' event and emits an 'ask' event with a question payload.

The web worker script creates an instance of Boss with self, which represents the web worker itself. It emits a 'connection' event to indicate the worker's connection, and it registers an event listener for the 'ask' event. Based on the received question, it emits an 'answer' event with an appropriate answer payload.

Both the main script and the web worker script can communicate with each other using the emit and on methods of the Boss instance.

Web Worker Boss API

new Boss(worker: Worker)

Creates a new instance of Boss with the specified web worker object.

  • worker: The web worker object to communicate with.

boss.on(event: string, callback: (payload: any) => void)

Registers an event listener for the specified event.

  • event: The name of the event to listen for.
  • callback: The callback function to execute when the event is received. It will receive the payload as a parameter.

boss.emit(event: string, payload: MessageEvent)

Emits an event to the web worker.

event: The name of the event to emit. payload: The payload to send along with the event.


That's it! You now have a better understanding of how to use the Web Worker Boss library for bi-directional communication between the main thread and a web worker in your web applications. Feel free to explore more features and advanced usage in the library's documentation or by examining the source code.

If you encounter any issues or have further questions, please don't hesitate to ask.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.