Get started with clientworker CDN

GPL-3.0-or-later licensed

JS worker processes run tasks asynchronously, communicate with main thread via messages.

Tags:
  • ServiceWorker
  • ClientWorker
  • Accelerate

Stable version

Copied!

How to start using clientworker CDN


// Import the ClientWorker library
const { ClientWorker, IS_BROWSER } = require('worker_threads').workerData;

if (IS_BROWSER) {
  // In the browser, create a new ClientWorker instance
  const clientWorker = new ClientWorker('worker.js', {
    // Pass any options you need
    foo: 'bar'
  });

  clientWorker.onmessage = (event) => {
    console.log('Message from worker:', event.data);
  };

  clientWorker.postMessage('Hello from main thread!');
} else {
  // In the worker thread, handle messages
  self.onmessage = (event) => {
    console.log('Message from main thread:', event.data);
    self.postMessage('Hello from worker thread!');
  };
}
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!

All versions