Additional information:
I'm new to angular and node.js.
I wan't to run a server with it's own events in a background process and tell it when to start and stop.
Problem description:
I know about the fork method from child_process but this does not work in angular with ts files since they are compiled to one big js file and when I use a js file I cant use my TypeScript classes.
I was reading about angulars component interaction which sounds like a parent child like process structure but they don't write whether it is.
Question:
So how can I use multi threading in angular 4 (to achieve what I described under additional information)?
Question update:
I tested the cluster module in angular and cluster.fork() does not run the angular module a second time, in other words so far I fail to get cluster working with angular.
The simple test function I used to test cluster in Angular (code is partly from the official example):
export function test() {
console.log(String(cluster.isMaster) + String(cluster.isWorker));
if (cluster.isMaster) {
cluster.fork();
cluster.on('death', function(worker) {
console.log('worker ' + worker.pid + ' died');
});
} else {
var bs: BluetoothServer = new BlenoBluetoothServer("12ab");
let counter = 0;
let discover = setInterval(() => {
counter++;
if (counter > 1) {
if (!bs.isAdvertising())
bs.startAdvertising();
}
}, 1000);
// Worker processes have a http server.
http.createServer(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(7777);
}
}
UPDATE 2
As a workaround I use child_process.fork() and add the script to webpack. After webpack bundling I'm trying to access a Class in the bundle but I get EntryPoint not defined. I'm following this answer.
So how can I access classes in my bundled code ?
The output config part of my webpack.config.js
"output": {
"path": path.join(process.cwd(), "dist"),
"filename": "[name].bundle.js",
"chunkFilename": "[id].chunk.js",
"library": "EntryPoint",
"libraryTarget": "var"
},
simple js file which I run in background:
const bundle = require('./polyfills.bundle.js');
var objectInstance = new bundle.EntryPoint.NobleBluetoothClient();
process.on('message', (message) => {
process.send("received message");
});
how I call it from Angular:
let clientSubprocess: ChildProcess = runJSinBackground(electronService);
clientSubprocess.on('message', (message) => {
console.log(String(message));
})
clientSubprocess.send("TEST");
The function in Angular which runs the fork:
export function runBluetoothClientInBackground(electronService: ElectronService):ChildProcess {
let clientSubprocess: ChildProcess = electronService.childProcess.fork(__dirname+'/scripts.bundle.js');
return clientSubprocess;
}
SOLUTION:
The problem was, that I was trying to make only one entry point (webpack entry point) accessible as a library, this is however not possible (or would require a hack).
You can add another configuration to the same webpack.conf
as described in this example.
You also need to require the webpack chunks in the correct order.
To find out in which order you need to load your chunks (if you have any) run webpack --display-entrypoints
.
Since these are multiple questions, it might be better to close this question.