Threads in Node.js with Web Worker
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
lib
src
tests
.gitignore
.gitlab-ci.yml
.travis.yml
CONTRIBUTING.md
LICENSE
README.md
binding.gyp
package.json

README.md

node-webworker

Web Worker implementation for Node.js. This module provides multi-threads programming for Node.js user-land by borrowing the concept of the standard Web Worker.

Get Started

'use strict';
const WebWorker = require('webworker-ng').WebWorker;
const worker = new WebWorker((self) => {
  self.postMessage('foobar', {yorkie: true});
  self.on('message', (data) => {
    console.log(data);
  });
}, {
  timeout: 5000,  // terminate after timeout
  defines: [],    // the parameters/objects passing to Worker
});

worker.postMessage({foo: 'bar'});
worker.onmessage = function(data) {
  // get message from worker
};

APIs

The node-webworker provides the class WebWorker for creating a separate worker.

Creating a webworker script

A WebWorker instance is corresponding a script to run, in low-level implementation, it creates a thread to sperate the v8 stack from the main.

The constructor WebWorker(script, options) accepts:

  • {Function} script the source code function which runs in worker. Will support the path to run.
  • {Object} options the options to config the worker
    • {Number} options.timeout the microsecond number to terminate forcily.
    • {Array} options.define the array of parameters to pass in worker context.

Messaging

In host environment, namely the Node.js, every WebWorker instance owns the method postMessage and the listener onmessage.

worker.postMessage('some data');
worker.onmessage = function(data) {
  // TODO
};

Correspondingly, in every worker context, it owns the same like the following:

const worker = new WebWorker(function(self) {
  self.onmessage = function(data) { .. };
  self.postMessage('some data sent from worker');
});

Collecting stdout and stderr

In worker context, the methods under console as console.log() and console.error are delegated by node-webworker which allows user to collect worker logs by worker as the following:

const worker = new WebWorker(function(self) {
  console.log('this sent to stdout');
  console.error('this sent to stderr');
});
worker.onstdout = function(msg) { ... };
worker.onstderr = function(msg) { ... };

Modular worker

In web worker, you can use the following builtin modules:

  • events - the Node.js official events mirror module
  • path - the Node.js official path mirror module

The other hand, you can also load your CommonJS file into your worker:

const worker = new WebWorker(function(self) {
  require('./tests/load-worker.js');
});

Note: The default path routing is from the process.cwd().

Installation

$ npm install webworker-ng --save

Tests

$ npm test

License

Apache License 2.0