MagicMirror² v2.13.0 is available! For more information about this release, check out this topic.

Solved - Several module instances with one node_helper


  • Project Sponsor Module Developer

    Hello all,

    I am a newby on development, but I would be pleased to learn ! 😀

    I am developping a module for Netatmo Presence camera (but my question is generic for all modules…) and I realize that when I am running several instance of the same module that is using a node_helper file, then the request is sent as much time as we have module instances declared and not just 1 request = 1 answer.

    To be more explicit :
    My MMM-Main.js request to node_helper.js to get some JSON data from an external API. And one of my parameter is the number of “events” that I want to return.
    If I have only 1 instance of my module, this is working perfectly fine and I receive exactly what I requested.
    But if I have 2 instances with the first one that request 10 events and the second request 2, then I receive 4 times the answer : 2 times with 10 (send to each module instance) and 2 times with 2 (send to each module instance also).
    And the more instance I declare, the more it increased !

    And this behavior totally mix up my result !

    So an easy solution will be to request always the max number of “events” and accept extra network request, but I don’t like it…

    Of course I checked all my “this” and “self” to ensure not to share datas between instances, so I don’t understand…

    I checked also on some other modules avaliable, almost all of them get redundant results when several instances are running together.

    Is there any explication and solution ?

    Thank you very much in advance for any help !

    AgP


  • Project Sponsor Module Developer

    Hi,

    I answer to myself as I could find the origin of the problem and correct it, maybe it could help others (as I didn’t see that implemented on many other module 😉 )

    So the problem is documented on the developper doc here :

    On socketNotificationReceived: function(notification, payload) part :

    Note 1: When a node helper sends a notification, all modules of that module type receive the same notifications.

    On this.sendSocketNotification(notification, payload) :

    Note: Since all instances of your module will receive the notifications, it’s your task to make sure the right module responds to your messages.

    So it is normal that each instance receive all the SocketNotification even send from another instance.

    To manage this, I add on start function :

    this.config.instanceID = this.identifier;
    

    As all my socket notification send to the note_helper “self.config”, so the node_helper will know the source of the request and will be able to send it back on its answer.
    Then on the node_helper sendSocketNotification, I send back this identifier with the corresponding answer.

    And on the MMM_main.js, on the socketNotificationReceived, I start to check is the identifier correspond to this instance and if not :

    return;
    

    Unfortunatelly this was even not enough, in particular at start-up, when all instances want to receive their datas and request all the node_helper together with differents inputs, some mix up occurs, so I also had to implement a “initialDelay” timer (with setTimeout function) and I will ask the user to configure it correctly if he configure several instances.

    So complicated solution (I feel…), but working fine !

    Of course if anyone got a better one to share, please do !

    Bye,
    AgP


  • Project Sponsor Module Developer

    Hi,

    I answer to myself as I could find the origin of the problem and correct it, maybe it could help others (as I didn’t see that implemented on many other module 😉 )

    So the problem is documented on the developper doc here :

    On socketNotificationReceived: function(notification, payload) part :

    Note 1: When a node helper sends a notification, all modules of that module type receive the same notifications.

    On this.sendSocketNotification(notification, payload) :

    Note: Since all instances of your module will receive the notifications, it’s your task to make sure the right module responds to your messages.

    So it is normal that each instance receive all the SocketNotification even send from another instance.

    To manage this, I add on start function :

    this.config.instanceID = this.identifier;
    

    As all my socket notification send to the note_helper “self.config”, so the node_helper will know the source of the request and will be able to send it back on its answer.
    Then on the node_helper sendSocketNotification, I send back this identifier with the corresponding answer.

    And on the MMM_main.js, on the socketNotificationReceived, I start to check is the identifier correspond to this instance and if not :

    return;
    

    Unfortunatelly this was even not enough, in particular at start-up, when all instances want to receive their datas and request all the node_helper together with differents inputs, some mix up occurs, so I also had to implement a “initialDelay” timer (with setTimeout function) and I will ask the user to configure it correctly if he configure several instances.

    So complicated solution (I feel…), but working fine !

    Of course if anyone got a better one to share, please do !

    Bye,
    AgP


  • Moderator

    Thanks for taking your time providing the solution you found. Very much appreciated


Log in to reply