npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

web-worker-boss

v1.0.0

Published

Communicate with your workers like a boss.

Downloads

3

Readme

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.