3

(If there is a question which answers this, please let me know)

I'm looking for suggestions for cross-browser XHR support, with-in workers.

In the normal VM, I usually use jQ for XHR stuff. It seems like overkill to load jQ into a worker, just for the AJAX support (assuming that's doable; haven't tried).

Suggestions?

Spot
  • 7,962
  • 9
  • 46
  • 55
  • 1
    Any browser which [supports Web workers](http://caniuse.com/#feat=webworkers) also supports [`XMLHttpRequest`](https://developer.mozilla.org/en/xmlhttprequest#Browser_compatibility) ([level 2](http://caniuse.com/#feat=xhr2)). A great feature of XHR2, namely the [`FormData`](https://developer.mozilla.org/en/DOM/XMLHttpRequest/FormData) object is undefined in Web workers. I've written an [implementation of `FormData` for Web workers](http://stackoverflow.com/a/10002486/938089?upload-a-file-in-a-google-chrome-extension) to fix this. So, `XMLHttpRequest` is sufficient for cross-browser workers. – Rob W Jun 22 '12 at 09:13

1 Answers1

3

I don't think there would be much point in firing a XML HTTP request from a web worker. After all, these requests are meant to be asynchronous! :)

Interestingly enough, someone suggested implementing support for XHR in web workers in the core of jQuery, but there's a very detailed response as to why it maybe isn't a good idea.

Some of his reasoning for why not to do it are:

  1. Spin up time for web workers is large
  2. Its not time efficient for single requests
  3. IE doesn't have very good support for web workers
  4. jQuery uses DOM methods throughout, meaning exceptions would be thrown when used in web workers

Quite a good read really

http://bugs.jquery.com/ticket/9889

Hope this helps!

Andy
  • 2,977
  • 2
  • 39
  • 71
  • Thanks. I'm looking for better ways to handle ultra low level operations. Thought workers might be a viable path. – Spot Jun 21 '12 at 17:13
  • Yeah, I think they're probably best for long running algorithms, maybe if you request a lot of data via ajax, a web worker may be useful for parsing it or processing it if it may take a while. I gets it's all trial, error and profiling in the end! :) – Andy Jun 22 '12 at 07:50