node-worker icon indicating copy to clipboard operation
node-worker copied to clipboard

addListener doesn't work inside the worker

Open notpushkin opened this issue 11 years ago • 0 comments

E. g. you can't do this:

// app.js
var NodeWorker = require("worker").Worker;
var worker = new NodeWorker("echo.worker.js");
worker.addListener("message", function (msg) { console.log(msg); });
worker.postMessage("world");
// echo.worker.js
var self = require("worker").worker;
self.addListener('message', function(e) { self.postMessage(e.data); }, false);

As you'll get this weird error:

ale@ale-Lenovo-B590 ~/Songbee/__sandbox__ $ node app.js 

undefined:1
undefined
^
SyntaxError: Unexpected token u
    at Object.parse (native)
    at Object.WorkerChild.handleMessage (__sandbox__/node_modules/worker/lib/worker.js:135:20)
    at Object.WorkerChild.handleData (__sandbox__/node_modules/worker/lib/worker.js:116:18)
    at Socket.<anonymous> (__sandbox__/node_modules/worker/lib/worker.js:63:10)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:736:14)
    at Socket.EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:408:10)
    at emitReadable (_stream_readable.js:404:5)
    at readableAddChunk (_stream_readable.js:165:9)

This seems a bit strange to me.

notpushkin avatar Aug 13 '14 21:08 notpushkin