5

I'm trying to incorporate JS Webworkers in my Lightning Component, but the Worker's initialization does not seem to find the specified javascript file:

var worker = new Worker('heavycalculations.js');

Although the specific file calculate.js is available as static resource and loaded using ltng:require, this does not mean that I can access the file in the component client controller.

I tried several ways to approach the file, but without luck:

var worker = new Worker('heavycalculations.js');
var worker = new Worker('/resource/heavycalculations/');
var worker = new Worker('/resource/heavycalculations');
var worker = new Worker('/resource/heavycalculations.js);

I tried it in different browsers, but it made no difference. I'm afraid using Webworkers will be a no-go for now, as it is not supported at all?

7

Web Workers do currently work in Lightning Components (and apps) but they represent a security attack vector that is possibly not going to be something we can overcome in the initial release of the upcoming Locker Service security model for Lightning Platform. We are discussing this inside of R&D actively but tomorrow is actually feature freeze for Summer'16 and we have not managed to find a solution yet. That does not mean we won't but I cannot guarantee we will before Summer'16 deploys.

With that in mind...

Here is a quick fully working example that uses a worker to multiply 2 numbers:

Static resource named webworker:

onmessage = function(e) {
  console.log('Message received from main script');
  var workerResult = 'Result: ' + (e.data[0] * e.data[1]);
  console.log('Posting message back to main script');
  postMessage(workerResult);
}

webWorkerDemo.app

<aura:application>
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
</aura:application>

webWorkerDemo.app.controller.js

({
    init : function(component, event, helper) {
        var worker = new Worker("/resource/webworker");
        worker.onmessage = function(e) {
            alert('Message received from worker: ' + e.data);
        };

        worker.postMessage([2, 3]);
    }
})

ltng:require does not factor into web workers - its sole purpose is to load scripts and css into the main page and web workers load their javascript contents directly. A number of your attempted script refs are nonsensical from static resource perspective - e.g heavycalculations.js alone is not a static resource reference at all, and the /resource/*.js also do not make sense since those are not valid developer names. What is the structure of heavycalculations? Is it a single javascript file or a js file in a zip archive?

  • Thanks! I don't know what went wrong, but when I built it up like this it worked fine. – westerforce Apr 11 '16 at 18:59
  • 1
    Will this solution work under Locker Service? I think "new Worker" is prohibited. Any workaround? – Maciek Simm Apr 13 '17 at 8:20
1

I don't think web workers work in Salesforce lightning.

Reference:

http://documentation.auraframework.org/lockerApiTest/index.app?aura.mode=DEV

Service worker and http client request

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.