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 = WebWorker;const worker = { self; self;} timeout: 5000 // terminate after timeout defines: // the parameters/objects passing to Worker; worker;worker { // 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.
- {Number}
Messaging
In host environment, namely the Node.js, every WebWorker
instance owns the method postMessage
and
the listener onmessage
.
worker;worker { // TODO};
Correspondingly, in every worker context, it owns the same like the following:
const worker = { self { }; self;};
stdout
and stderr
Collecting 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 = { console; console;};worker { ... };worker { ... };
Modular worker
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 = { ;};
Note: The default path routing is from the process.cwd()
.
Installation
$ npm install webworker-ng --save
Tests
$ npm test