Socket
Socket
Sign inDemoInstall

comlink

Package Overview
Dependencies
Maintainers
3
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

comlink

Comlink makes WebWorkers enjoyable


Version published
Weekly downloads
386K
decreased by-1.78%
Maintainers
3
Weekly downloads
 
Created

What is comlink?

Comlink is a utility that simplifies the process of working with Web Workers and other forms of remote communication in JavaScript. It allows you to expose and call functions across different execution contexts, such as between the main thread and a Web Worker, as if they were local functions.

What are comlink's main functionalities?

Basic Usage

This example demonstrates the basic usage of Comlink to expose an object in a Web Worker and call its methods from the main thread.

const { wrap } = require('comlink');

// worker.js
const obj = {
  counter: 0,
  increment() {
    this.counter++;
    return this.counter;
  }
};
Comlink.expose(obj);

// main.js
const worker = new Worker('worker.js');
const remoteObj = wrap(worker);
(async () => {
  console.log(await remoteObj.increment()); // 1
  console.log(await remoteObj.increment()); // 2
})();

Transferable Objects

This example shows how to use Comlink with transferable objects, such as ArrayBuffers, to efficiently transfer data between the main thread and a Web Worker.

const { wrap, transfer } = require('comlink');

// worker.js
const obj = {
  processArray(buffer) {
    // Process the buffer
    return buffer;
  }
};
Comlink.expose(obj);

// main.js
const worker = new Worker('worker.js');
const remoteObj = wrap(worker);
const buffer = new ArrayBuffer(8);
(async () => {
  const result = await remoteObj.processArray(transfer(buffer, [buffer]));
  console.log(result); // ArrayBuffer(8)
})();

Class Instances

This example demonstrates how to use Comlink to expose class instances in a Web Worker and interact with them from the main thread.

const { wrap } = require('comlink');

// worker.js
class Counter {
  constructor() {
    this.value = 0;
  }
  increment() {
    this.value++;
    return this.value;
  }
}
Comlink.expose(Counter);

// main.js
const worker = new Worker('worker.js');
const RemoteCounter = wrap(worker);
(async () => {
  const counter = await new RemoteCounter();
  console.log(await counter.increment()); // 1
  console.log(await counter.increment()); // 2
})();

Other packages similar to comlink

FAQs

Package last updated on 02 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc