MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Solved - Several module instances with one node_helper

    Scheduled Pinned Locked Moved Solved Development
    3 Posts 2 Posters 1.1k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A Offline
      AgP42 Project Sponsor Module Developer
      last edited by AgP42

      Hello all,

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

      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

      1 Reply Last reply Reply Quote 0
      • A Offline
        AgP42 Project Sponsor Module Developer
        last edited by

        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 :winking_face: )

        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

        1 Reply Last reply Reply Quote 2
        • yawnsY Offline
          yawns Moderator
          last edited by

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

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Enjoying MagicMirror? Please consider a donation!
          MagicMirror created by Michael Teeuw.
          Forum managed by Sam, technical setup by Karsten.
          This forum is using NodeBB as its core | Contributors
          Contact | Privacy Policy
            OSZAR »