0

So i'm trying to make two node.js processes communicate. Here is a quick exemple of what i'm trying to do :

Process1.js

var process2 = require('./process2');

class Process1 {

    constructor() {
        this._value = process2.getValue();
        this.value = [];
    }

    addValue(_value) {
        this.value.push(_value);
    }

}

Process2.js

var process1 = require('./process1');

class Process2 {

    constructor() {
        this.value = "Hello";
    }

    getValue() {
        process1.addValue(this.value);
    }

}

I know this code can be done easily and does not require to be in two separate files ... But it's just an example.

I've tried using FORK but since it's an "circular" process loop …

If anyone has any idea on what I can do to have those two process working with each other, that would be kindly appreciated :)

  • `process2.getValue()` should be replaced with `this.addValue('Hello');` – Lewis Apr 25 '18 at 09:55
  • I know that this can be done, this was just an example ... I need to have the two process communicate with each other – Corsica Apr 25 '18 at 09:56
  • Whenever you have issues with circular dependencies, then you should think about rewritting your code instead. – Lewis Apr 25 '18 at 09:59

1 Answers1

0

You should read some articles about Cyclic Dependencies.

This one for example is in my opinion quite good: http://blog.cloudmineinc.com/managing-cyclic-dependencies-in-node.js

For this one, you can use Dependency Injection, which means you require both from one script, and give an instance of Process1 to Process2.getValue and an instance of Process2 to the Process1-constructor.

NeedCoffee
  • 70
  • 2
  • 9