Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
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.
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
})();
Workerize is a library that automatically moves a module into a Web Worker and proxies calls to it. It is similar to Comlink in that it simplifies the use of Web Workers, but it is more focused on automatically converting entire modules into workers, whereas Comlink provides more granular control over exposing and calling individual functions or objects.
Threads is a library for creating and managing Web Workers with a focus on simplicity and type safety. It provides a higher-level API compared to Comlink, with built-in support for TypeScript and more structured worker management. While Comlink offers more flexibility in exposing and calling functions, Threads aims to provide a more streamlined and type-safe experience.
Greenlet is a micro-library that allows you to run a function in a Web Worker and return a promise for its result. It is similar to Comlink in that it simplifies the use of Web Workers, but it is more lightweight and focused on running individual functions rather than exposing entire objects or classes.
FAQs
Comlink makes WebWorkers enjoyable
The npm package comlink receives a total of 313,612 weekly downloads. As such, comlink popularity was classified as popular.
We found that comlink demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.