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.
'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
};
The node-webworker provides the class WebWorker
for creating a separate worker.
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.
- {Number}
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');
});
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) { ... };
In web worker, you can use the following builtin modules:
events
- the Node.js officialevents
mirror modulepath
- the Node.js officialpath
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()
.
$ npm install webworker-ng --save
$ npm test