workerstream-channel
v0.0.2
Published
use HTML5 web workers with the node stream API
Downloads
1
Readme
workerstream
npm install workerstream
use HTML5 web workers with the node streams API
var workerstream = require('workerstream')
var worker = workerstream('my-worker.js')
worker
is a stream and speaks stream events: data
, error
and end
. that means you can pipe worker output to anything that accepts streams, such as an XHR. you can also pipe data into workers (such as a webcam feed or audio data)
example
in your app:
var worker = workerstream('worker.js')
worker.on('data', function(data) {
console.log(data)
})
worker.on('error', function(e) { console.log('err', e)})
worker.write({ hello: 'world' })
the worker code (worker.js
above):
self.onmessage = function(event) {
self.postMessage({whats: 'up'})
}
you can also pass in existing webworker instances
channels
in your app:
var worker = new Worker('worker.js')
var workerStream = workerstream(worker, 'channel1')
var workerStream2 = workerstream(worker, 'channel2')
workerStream.on('data', function(data) {
console.log(data)
})
workerStream.on('error', function(e) { console.log('err', e)})
workerStream.write({ hello: 'world' })
workerStream2.write({ goodbye: 'world' })
the worker code (worker.js
above):
var parentStream = ParentStream(self, 'channel1')
workerStream.on('data', function(data) {
console.log(data)
// Outputs only { hello: 'world' } message
})
using with webworkify
webworkify allows you to simply create browserified webworkers.
var WebWorkify = require('webworkify')
var WorkerStream = require('workerstream')
var worker = WebWorkify(require('./worker.js'))
var workerStream = WorkerStream(worker)
Your worker.js
can use this module's ParentStream
to create a stream connecting back to the parent
var ParentStream = require('workerstream/parent')
module.exports = function(){
var parentStream = ParentStream()
parentStream.pipe(somewhereAwesome).pipe(parentStream)
}
transferable objects
worker.write(arraybuffer, [arraybuffer])
MIT LICENSE