Skip to content

WebReflection/node-worker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-worker: DEPRECATED thanks to coincident 🎉

License: ISC donate

Web Worker like API to drive NodeJS files

npm install @webreflection/node-worker

Concept

The aim of this project is to provide an alternative to Electron environment. This might be particularly useful in those platforms with constrains such Raspberry Pi Zero or 1.

The module is based on the standard Web Worker API.

You can postMessage(data) and receive onmessage = (data) => {} on both client and server.

All workers files must be inside a workers directory within the application folder.

Every worker is a sandboxed VM and it runs on the backend: nothing is shared directly with the browser.

Basic Example

NodeJS

var index = require('fs').readFileSync(__dirname + '/index.html');

var http = require('http').createServer(handler);
var nodeWorker = require('@webreflection/node-worker');

var app = nodeWorker(http);
app.listen(process.env.PORT);

function handler(req, res) {
  res.writeHead(200, 'OK', {
    'Content-Type': 'text/html'
  });
  res.end(index);
}

Express

var index = require('fs').readFileSync(__dirname + '/index.html');

var express = require('express');
var nodeWorker = require('@webreflection/node-worker');

var app = nodeWorker(express());
app.get('/', handler);
app.listen(process.env.PORT);

function handler(req, res) {
  res.writeHead(200, 'OK', {
    'Content-Type': 'text/html'
  });
  res.end(index);
}

Demo index.html

<!doctype html>
<!-- socket.io must be available before /node-worker.js -->
<script src="/socket.io/socket.io.js"></script>
<script src="/node-worker.js"></script>
<script>
var nw = new NodeWorker('echo.js');
nw.postMessage({hello: "world"});
nw.onmessage = function (event) {
  console.log(event);
};
nw.onerror = function (error) {
  console.error(error);
};
</script>

workers/echo.js

// simple echo
// when some data arrives
// same data goes back
onmessage = function (e) {
  postMessage(e.data);
};

You can clone and run npm test after an npm install.

About

Web Worker like API to drive NodeJS files

Resources

License

Stars

Watchers

Forks

Packages

No packages published