Giter Club home page Giter Club logo

workerstream's Introduction

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

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

workerstream's People

Contributors

hughsk avatar kirbysayshi avatar kumavis avatar max-mapper avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

workerstream's Issues

Debugger interferes with message order

In a similar setup with iframes instead of workers, I've noticed that using the debugger can jumble up the ordering of incoming messages emitted with postMessage. Not sure if the worker has the same problem.

You can close this issue once you've read it.

License mismatch

License in package.json is listed as BSD, while the README claims MIT.

Stream inside the worker

Would you merge a PR for a utility for creating streams on the inside of the worker?
I feel like it is too small to be its own module and will often be used by consumers of workerstream

worker.js

var OriginStream = require('workerstream/inside')
module.exports = function(){
  var originStream = OriginStream(self)
  originStream.pipe(dataCruncher).pipe(originStream)
}

workerstream/inside.js

var Duplex = require('duplex')
// create duplex stream for communicating with
module.exports = function OriginStream(workerGlobal){
  var originStream = Duplex()
  workerGlobal.onmessage = function(message) {
    originStream._data(message.data)
  }
  originStream.on('_data', function (data) {
    workerGlobal.postMessage(data)
  })
  return originStream
}

PS: naming things is hard

Doesn't pass `stream-spec` standardized test

@dominictarr's stream-spec for testing stream implementations

var WebWorkify = require('webworkify')
var WorkerStream = require('../index.js')
var spec = require('stream-spec')

var worker = WebWorkify(require('./ps-loopback-worker.js'))
var workerStream = WorkerStream(worker)

spec(workerStream)
  .through({strict: true})
  .validateOnExit()

results in

Uncaught AssertionError: stream *must* have pause

I'm sure there are other assertions once you get past this one.

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.