GithubHelp home page GithubHelp logo

willcode2surf / node-webworker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pgriess/node-webworker

0.0 2.0 0.0 141 KB

A WebWorkers implementation for NodeJS

License: BSD 3-Clause "New" or "Revised" License

node-webworker's Introduction

node-webworkers is an implementation of the Web Workers API for node.js.

See the design document here.

Example

Master source

var sys = require('sys');
var Worker = require('webworker');

var w = new Worker('foo.js');

w.onmessage = function(e) {
    sys.debug('Received mesage: ' + sys.inspect(e));
    w.terminate();
};

w.postMessage({ foo : 'bar' });

Worker source

onmessage = function(e) {
    postMessage({ test : 'this is a test' });
};

onclose = function() {
    sys.debug('Worker shuttting down.');
};

API

Supported API methods are

  • postMessage(e) in both workers and the parent; messages are in the parent if this is invoked before the child is fully initialized
  • onmessage(e) in both workers and the parent
  • onerror(e)in both workers and the parent
  • terminate() in the parent

In addition, some nonstandard APIs are provided

  • onclose() in the worker (allows for graceful shutdown)
  • The postMessage() method takes an additional optional file descriptor parameter, which will be sent with the message. This descriptor will be passed to onmessage handlers as an optional fd field. Handlers receiving messages posted without file descriptors will not see an fd field. Both the parent and child can send file descriptors using this mechanism.
  • Worker.onexit(code, signal) in the master, which is invoked on the master Worker object when the worker process exits.
  • The Worker constructor takes an additional optional object argument, opts, which is used as a dictionary of options with the following keys
    • args : A string or array of strings to pass to the executable before the filename to invoke. This can be used to request that the worker start up in debug mode (e.g. { 'args' : '--debug-brk' }). By default this is empty.
    • path : A string naming the executable to invoke for workers. By default this is the value of process.execPath (e.g. node or similar).

Installation

This package can be installed via npm as follows

% npm install webworker

Note that this requires node-websocket-client v0.9.3 or later. This dependency will be handled automatically by npm, but must be dealt with manually if installing using another procedure.

Credits

This package contains a static snapshot of Micheil Smith's excellent node-websocket-server with some fixes applied to handle UNIX sockets.

node-webworker's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.