DynWorker by Félix Saparelli

Web threading made easy

sandbox loading...

The above sandbox is already set up, so you can just dive in.

Be aware that there's some functions documented below that are not implemted yet.

Single-player Pong


To create a worker, use the DynWorker object:

var thread = new DynWorker;

For now, you'll need to keep track of that variable, but we'll see how you can forgo that later on with more advanced tricks.


There's two ways of destroying a worker. You can politely ask it to commit seppuku or you can just put it under the guillotine...


Pushing & Pulling

You can actively push & pull data to & from the worker. Data is defined as being any JSON type plus functions. Functions within objects are not yet supported.

thread.push('name', 'data');

The 'name' is where the data will be stored (which is just straight on the in-worker global object: self).

Pulling works the same way, and it takes a function which is passed the pulled data.

thread.pull('name', function (data) {});

Everything is asynchronous, but this is inter-thread communication, so it's not like you're ever going to notice anything below near-instantaneous speeds.

Running a function

After you've pushed a function into the worker, you can call it from the parent thread and pass in arguments. The same restrictions as push applies to the arguments, so be wary.

thread.run('func_name', args...);

Listen for messages

This will filter all internal communications, so it won't break your algos with weird looking data.

thread.listen(function (msg) {});

You can bind as many functions as you need or want to; they'll all get the same messages.

Eval shit


That just runs eval inside the worker. No frills. No checks. You're on your own.

Reverse psycho

All these methods are also available from within a worker on the special parent_thread global. They reverse the roles and allow data and code to be pushed, pulled, ran, and evaluated in the parent thread from the worker. (This is potentially very dangerous if you allow unchecked code to run within a worker. So don't.)

The lim(x→∞) workers

Things start to get interesting when you get to use multiple workers at the same time. You do this using "classes" and the awesome Workers object.

Simple grouping

A basic example uses the Worker object to group three existing workers together and push the same data to all of them in one go:

var groupies = Workers(thread1, thread2, thread3);
groupies.push('name', ['some', 'data', 'here']);


You can assign class(es) to specific workers, and act upon all workers with that class. It makes for some interesting uses.

Workers(thread1, thread2).addClass('fanclub');
Workers('.fanclub').run('cheers', "Woooo!", "Yeah!");

You can also remove a class or check for existence, jQuery-style:

thread.addClass('class'); // Add
thread.hasClass('class'); // => true
thread.removeClass('class'); // Remove
thread.hasClass('class'); // => false

Weapon of mass creation

If you want to have 6 workers crunching numbers, it's a pain to create them individually. So there's a helper for that:


What have we done? We've gotten an empty set, added 6 new workers to it, and finally tagged them with the crunch class. Ready for further use.

Workerset manipulation

// Add...

// Remove...

// Get the first non-rodent furry...

// This is a mice trap:


Not implemented (but you're welcome to try!). Proposed API:

Workers.pub('channel', data);
Workers.sub('channel', function (data) {});

From people, to people

DynWorker is in its third iteration, stable. It was developed with love by and is open-source (Public Domain). Go check it out on GitHub.

It is currently unmaintained, but I am always happy to review pull requests!


The headers' font is Passero One by Viktoriya Grabowska.