Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Computing with JavaScript Web Workers (ejohn.org)
63 points by emontero1 on July 22, 2009 | hide | past | favorite | 26 comments


Javascript just keeps getting cooler and cooler. The more I learn about it the more I find myself loving it.


I can only second that, also since JS is runnable Serverside you can do alot of funny things.

http://en.wikipedia.org/wiki/Server-side_JavaScript


I have a feeling that server-side js will eventually become a major player in the web world, especially if solid, reliable implementations are created. The idea of having a uniform language to communicate between the server and client with a decent data transportation format (JSON) has been something I've long for, enough so that one of my New Year's resolutions was to start contributing to a SS-JS implementation this year.


In that case, can I enlist your services :)

I started Narwhal (a JavaScript standard library: http://narwhaljs.org) and Jack (a WSGI/Rack-like web server interface: http://jackjs.org) earlier this year with the intention of bringing JavaScript up to par with Ruby, Python, etc.

There are other nice SSJS products, but they're all very monolithic and self-contained (in a bad way). I'm hoping Narwhal will be more modular and open, like we have with most other languages.


I'm tracking your two projects on github and have also been watching ry's node project. I'm really interested in how SS-JS will develop. For one it necessarily removes the DOM model people are familiar with and it also has evolved in an environment that has fostered the growth of many interpreters without one being totally dominant. V8 and Rhino are both very manageable, it will be interesting to see how compatible they all remain and if one assumes the position as the de-facto standard.


I am really looking to use V8 more then anything. Rhino is nice but the momentum is with V8. The issue with those is that a feature is Java library support and I personally would prefer native libs then some hack to make Java libs work.


So one of the cool things about Narwhal is it was created with the goal of supporting multiple interpreters. Where possible, code is written in JavaScript to facilitate sharing between interpreters. For example, the "binary" module is currently about 700 lines of JavaScript, plus about 100 lines of C/C++ or JavaScript per platform.

While Rhino is currently the most complete Narwhal platform (mostly due to the ease of prototyping in it) We've also started working on V8 (github.com/tlrobinson/narwhal-v8/) and JavaScriptCore (github.com/tlrobinson/narwhal-jsc) support. They're still very incomplete but it's a start.


I asked a similar question some time ago - whether Javascript will become the long sought after unifying language of web development. I was surprised that the other option seemed more appealing to people - writing interpreters/compiler in Javascript for other languages which can then run in the browser.

For example see Flapjax (http://www.flapjax-lang.org)


I am waiting for that day, and I'm willing to collaborate on any project that bring JS closer to unify web development.


i'll be happier when john et al push through the next major lang revision

this clinginess to js strikes me of stockhold syndrome. people are working themselves into a lather as to how awesome and powerful it is, but most of this power is rooted in crappy hacks. namespaces for example. or crockfords currying stuff. very hacky.

js will do. it gets the job done. would i choose to code on the server side with it over python, perl, or haskell? nope


Web Workers look ugly to me :( Sad that they thought of this as a good idea. It's just a horrible hack that'll lead to horrible code.

Threads are usually misused. When you add threads and it speeds up your code, you're usually doing something terribly wrong (When #threads>#cores).


I don't really understand what you're upset about - you don't like how they look and you're worried that they're a hack? Are you familiar with the current situation of processing in JavaScript? It involves running chunks of code, split up using delayed timers, that constantly block the user interface from being usable. If you want to talk about something that doesn't look good and is hacky - that's what you should be railing against.


That current situation is basically extremely powerful. It's effectively doing your own time slicing. In terms of code, to me, it's pretty.

IMHO it'd be far nicer just to speed js up and perhaps create some helper functionality to aid in splitting chunks of work up into bite size pieces, and make it more optimal.

It'd be much nicer to just have some yield statement which allows you to say "Hey, it's ok if you process any pending UI events, timers etc etc here". That way you could just have a single main loop with some yields in the right places. It'd create simpler, more maintainable code, and there's no reason it'd be slower or less responsive UI wise than the web worker version.

I can see the examples where web workers can be useful, where you're doing really CPU heavy backgroud work such as image processing etc. But I think people will end up using web workers for far more than that, which will make for some ugly code (IMHO).


This looks promising. I'm sorry I'm late to the game with the postMessage API (first time I've seen it). But I have a question about that. How does it compare to, say, Cocoa's NSNotificationCenter style of messaging, in which there is a default message center that handles receiving and dispatching? From my really brief look at it, it seems similar with one caveat: you can have multiple NSNotificationCenters. So observers don't have to observe all messages. But here it looks like every observer will be observing all messages sent. Is that correct?

As you point out here (http://ejohn.org/blog/postmessage-api-changes/) this will lead to a potential security issue in which you might be responding to messages posted by an external source. Maybe another property could be added, which I'll refer to as messagingDomains. This could be set to a string like 'http://example.com or an array of strings. Each string corresponds to a domain. If this property is set the onmessage would only be called if the origin was from the domain(s) specified in messagingDomains. It's a subtle addition to the API, yes. But it would more clearly establish the existence of a security risk here, because it would probably be propagated better in documentation than a warning to conditionally check event.origin.

For reference, here is how onmessage works right now:

observer.onmessage = function(e) {

  if (e.origin == 'http://example.com') {

    // I trust this domain, do something

  }
}

Here is what I think would make sense to clearly establish that security matters here, and as a benefit it would not break backward compatibility:

observer.messagingDomains = ['http://one.example.com, 'http://two.example.com];

observer.onmessage = function(e) {

  // By default execute accordingly.

  // However, if messagingDomains is set, only execute if the domain is mentioned explicitly

}


So what better solution are you proposing?


Gripes:

* All communication is done via strings. This is really a hack which takes advantage of the immutability of strings in JS. Maybe some kind of queueing/message passing system would be better but that is another can of worms. * Loading workers via a separate file is nice for the web, and I get the point that it makes sense for keeping workers out of the global namespace. However, I would have liked to see something function (or even object) based.

All that being said. I think it is a good start and is coming at the concurrency problem from the right angle (i.e. no threads/locking).


So now any website could setup a web worker in the background of pages to massively improve there processing power.

Very cool feature though, makes the entry level to distributed processing very low.


How long until some high-traffic page owner decides to resell his visitors CPU time ?


I'm rather amazed that this is available in Firefox and Safari today.


Running the demo caused my Safari to crash


tragedy of the commons. wow, i just can't wait for every amateur web designer to take up the mantle of threaded coding.

get used to "kill -9", you're going to be using it to preempt all this amateur-hour worker code people slap together and foist on you


I can't say I agree with that. Actually I think that an easily-accessible code interpreter sitting inside a browser, with a language and runtime capable of delivering spectacular stuff in just a few lines can nurture a future generation of programmers, not unlike the generation raised on easily accessible basic interpreters on various 8-bit computers in the 70s and 80s. I don't think denying access for beginners to powerful but harmless tools (it sits inside a browser) is a good idea, how could they become experienced professionals such as you then?


Why web workers can't take a function or an object?

  myobject={
    start:      function(){ /* do stuff */ }
    stop:       function(){ /* end worker */ }
    postMessage:function(){ /* communicate */ }
    onmessage:  function(){ /* receiving data*/ }
    onerror:    function(){ /* handle stuff */ }  
  }
  
  myworker = new Worker(myobject);
  myworker.start();
  myworker.postMessage("dostuff");
  myworker.stop();
Or something like that...


That can't work because the individual methods may contain a closure to a non-threadable object (like the DOM). The reason why all of the logic is forced into in an external script is to prevent contention from occurring - your proposed solution definitely would not allow for that.

On the other hand, allowing for an easy way to call global functions of the child worker would be really nice.

   var worker = new Worker("worker.js");
   worker.start();
Child Worker:

   start = function(){
     // Gets called by the parent
   };
That would be sexy and surely simplify a lot of logic surrounding message passing.


Point taken, then let me rephrase that.

Threads we can fire and forget. Threads we can start "inside" our script and let them work while we do other stuff.

  myfunnyfunc = function(){/* do long funny stuff */}
  mycallback  = function(){/* do something when done */}
  
  mythread = new Thread(myfunnyfunc,mycallback);
Asynchronous like xmlhttprequest, just a way to replace the "settimeout" hack.


Taking a lok at it, sending messages back and forth is retarded. Just call the damn functions by their names with their arguments since the worker itself is already threaded and sandboxed.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: