DailyJS

DailyJS

The JavaScript blog.


Tagwebworkers
Featured

webworkers ES6 generators

AsyncTask, DefineJS ES6 Features

Posted on .

AsyncTask

Web workers are great, but who wants to go to the trouble of creating extra files just to run some code in parallel? AsyncTask (GitHub: gorillatron/async-task, License: MIT, npm: async-task) by Jørn Andre Tangen is a more generic web worker constructor that lets you define background tasks purely with JavaScript:

var task = new AsyncTask({  
  doInBackground: function(a, b) {
    return a + b
  }
});

task.execute(1, 2)  
  .then(function(result) {
    result === 3
  })
  .catch(handleException);

It has a fluent API, and has Karma/Mocha unit tests as well.

DefineJS: ES6 Generator Syntax

Recently I wrote about Mehran Hatami's define.js project, which is a lightweight AMD implementation. It has just been updated to support ES6 generators for loading modules, which means you can do this:

var module = yield require('module');  

It's still asynchronous, but it looks synchronous. I think this is a really cool use of generators, and apparently several people asked Mehran to implement this so it seems like there's demand for it.

Featured

node modules webworkers p2p crypto

P, EasyWebWorker, OpenPGP.js

Posted on .

P

P (GitHub: oztu / p, License: Apache 2, npm: onramp, bower: p) by Ozan Turgut is a client-side library with a WebSocket server for creating P2P networks by allowing browser-to-browser connections.

The onramp Node module is used to establish connections, but after that it isn't necessary for communication between clients. The author has written up documentation with diagrams to explain how it works. Like other similar projects, the underlying technology is WebRTC, so it only works in Chrome or Firefox Nightly.

EasyWebWorker

EasyWebWorker (GitHub: ramesaliyev / EasyWebWorker, License: MIT) by Rameş Aliyev is a wrapper for web workers which allows functions to be executed directly, and can execute global functions in the worker.

A fallback is provided for older browsers:

# Create web worker fallback if browser doesnt support Web Workers.
if this.document isnt undefined and !window.Worker and !window._WorkerPrepared  
  window.Worker = _WorkerFallback

The _WorkerFallback class is provided, and uses XMLHttpRequest or ActiveXObject.

The source code is nicely commented if you want to look at what it does in more detail: easy-web-worker.coffee.

OpenPGP.js

Jeremy Darling sent in OpenPGP.js (GitHub: openpgpjs / openpgpjs, License: LGPL), which is an OpenPGP implementation for JavaScript:

This is a JavaScript implementation of OpenPGP with the ability to generate public and private keys. Key generation can be a bit slow but you can also import your own keys.

Jeremy found that OpenPGP.js is used by Mailvelope, which is a browser extension that brings OpenPGP to webmail services like Gmail. That means Mailvelope can encrypt messages without having to upload a private key to a server.

Featured

libraries events webworkers

LNUG, Supplement.js, console4worker

Posted on .

LNUG

The London Node.js User Group occurs on the last
Wednesday of every month, in Camden. This month's event is on the 28th
of September at 6pm, and has four talks scheduled:

  • Tom Hall - Webscaling With Node.js
  • Andy Kent - Streaming Analytics and Node.js
  • Rob Tweed - The Globals Database: It's Significance To Developers
  • Garren Smith - These are the ORMs you are looking for

There's a Google Group for keeping up to date with the group: LNUG
Google Group
.

Supplement.js

Supplement.js (GitHub: olivernn / supplement.js) by Oliver
Nightingale adds extensions to built-in types:

[Supplement.js is] a small collection of utility functions to make working with JavaScript that much sweeter and more expressive. Responsibly adds extensions to built in types.

There are some nice functional style methods, like
Array.prototype.head, Array.prototype.tail,
Array.prototype.take, and a lot of functions you probably
use from Underscore.js.

The Function extensions are interesting too:
Function.prototype.throttle for example returns a function
that will only execute once every n milliseconds.

console4Worker

console4Worker (GitHub: jeromeetienne / console4Worker,
License: MIT) by Jerome Etienne helps debug WebWorkers by making
console.log (and other console methods) work
inside workers in browsers that don't yet support this.

Some configuration within each worker is required first:

importScripts('console4Worker-worker.js');
console.log("console call made from inside a webworker");

Then on the actual page:

// init the worker
var worker  = new Worker("worker.js");
// bind the console4Worker to get console API from worker
console4Worker.bind(worker);

worker.addEventListener('message', function(event) {
  // filter this event if it is from console4Worker
  if (console4Worker.filterEvent(event)) {
    return;
  }

  // ... here handle your own events
}, false);

Featured

graphics mobile webworkers basic

Distributed Mandelbrots, What to Read, 1k Christmas Tree, Mobile Best Practices

Posted on .

Distributed Mandelbrot Generation

When I was at university my dissertation was on distributed computing
with arbitrary-precision arithmetic. Which basically meant I used an
entire lab to generate pretty pictures with very large numbers.

So when I found
mandlebrotapp.appspot.com I got all misty-eyed and nostalgic. It uses browsers to generate sections of
the Mandelbrot set. It has a basic job management system so you can
submit new jobs, and it'll use different colours during rendering so you
can see which blocks other people generated.

Given the news of recent password leaks from major sites, if anyone has
the guile to create a rainbow table hash cracker using JavaScript
delivered over ad networks the Internet will probably fall apart. Don't
do that! Unless it's purely for research purposes, of course.

What to Read to Get Up to Speed in JavaScript

What to Read to Get Up to Speed in JavaScript
by Rey Bango is list of books and blogs for beginner, intermediate, and
experienced JavaScript developers. It mentions the classics, like
JavaScript: The Good Parts, and even includes DailyJS (thanks!)

1k Christmas Tree

More animated weather effects. This is becoming a DailyJS meme now.
This animated Christmas tree by @romancortes uses Canvas to render a rotating 3D tree, complete with falling snow.

Mobile Web Application Best Practices

Mobile Web Application Best Practices is a W3C
Recommendation published on 14th December, 2010. It consists of a list
of 32 best practices, with explanations for each point:

Use Cookies Sparingly

Cookies are a common and effective means to store small amounts of state on the client. [...] Information stored in cookies, however, is sent to the server for every request and so using them for excessive amounts of data can negatively impact performance, particularly on a mobile network.

I actually think this would be a great resources for developers new to
mobile web development, and even useful as a sanity check for more
experienced developers and designers.