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.

    when to use init , start, loaded of node helper

    Scheduled Pinned Locked Moved Development
    9 Posts 3 Posters 1.2k Views 3 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.
    • M Offline
      MMRIZE @kayakbabe
      last edited by MMRIZE

      @kayakbabe

      You can ignore .init() or .loaded(). Usually, you don’t need to touch these methods of nodeHelper class. (Anyway, you can use .init() for doing something on creation time.)

      When the module is loaded and then started, if there exists node_helper.js in the directory of the module, node_helper.js of each module is configured (express and socketIO), and then .start() will be executed. (After "Server started ..." log)

      So you can initialize your node_helper in .start() method. You can define/assign some properties to use in your node_helper script. Of course you can start your process here.

      To communicate with its front MM module, you must use socketNotification to get some values from the module(.socektNotificationReceived()), or to send some values to the module(this.sendSocketNotification()). .socektNotificationReceived() would be where the main conditional job triggers start.

      If you describe in more detail what your module shall do, a better answer would be possible.

      1 Reply Last reply Reply Quote 0
      • S Do not disturb
        sdetweil @kayakbabe
        last edited by

        @kayakbabe I agree w @MMRIZE , probably never user init or loaded.

        think of start() as where u might do one time things before real work begins… like initialize hardware, or some such

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        kayakbabeK 1 Reply Last reply Reply Quote 0
        • kayakbabeK Offline
          kayakbabe @sdetweil
          last edited by kayakbabe

          @sdetweil if start is for one time things, where would I do work that must be repeated endlessly like a sensor or button that needs to be constantly checked.

          If I have to init a node package that my module uses, I do that in start? so setting up in module variables is done in start?

          S 3 Replies Last reply Reply Quote 0
          • S Do not disturb
            sdetweil @kayakbabe
            last edited by sdetweil

            @kayakbabe in start you would create a timed cycle using setInterval, or a timer using setTimeout.

            basically, node help gets called once by mm

            and then is dependent on notifications for it’s work process. it’s JOB is to help it’s module, by doing things the module can’t do directly

            access hardware, read files, call libraries that might do those things (accessed via require())

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            1 Reply Last reply Reply Quote 0
            • S Do not disturb
              sdetweil @kayakbabe
              last edited by

              @kayakbabe

              yes you could init a library in start. but, you might not have the info you need, that would/should have been placed in config on the module in config.js

              that info was passed to the modulename.js.

              you will see that modules that use a node helper send a notification early on to pass the config object to the helper. this happens effectively light years after start is called (maybe seconds)

              so generally start isn’t used much, as the helper is dependent on the config info usually stuff like
              gpio pins, file names, urls, usernames and passwords… cycle time limits…

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              1 Reply Last reply Reply Quote 0
              • S Do not disturb
                sdetweil @kayakbabe
                last edited by

                @kayakbabe think of the helper as a web service provider

                gets a request (notification)
                process
                send response(notification)

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                1 Reply Last reply Reply Quote 0
                • kayakbabeK Offline
                  kayakbabe
                  last edited by

                  @sdetweil your explanations helped a lot. I debugged using a timer in the node_helper to fake detecting different types of button press and my module will send magic mirror notifications. (So, if I can detect the actual physical events I’m pretty sure my script will work.

                  I think it’s the node package I’m trying to use that is the problem.

                  I even got so far as to figure out that a dependency of the package I want to used wasn’t compiled for Node version 16. which i what I’ve got running on my Mirror pi. I figured out it is c++ and even figured out how to recompile it with node-gyp and make. so now I do not get any errors in the pm2 error log. but now instead of a black screen electron, I don’t get electron opening a window at all. I check the PM2 logs. There are no errors in the mm-error log. It is blank.
                  However, the pm2 mm-out log is being added to over and over, so electron and MagicMirror are running. and the error keeps being repeated in the mm-out log.

                  The error which i’ve googled and gotten no working result for is:

                  ERROR Error: Cannot find module 'bindings'. 
                  

                  Yet there is a bindings folder in the node_helper folder.
                  Is there a way to find out what path is being used to try to find “bindings”.

                  Or maybe the package I’m trying to use just is too much of a mess to keep screwing with. It sure seems like it would be super useful. I wanted to add a few physcial intermittant buttons and be able to assign multiple uses to them. Funny thing is… it works great when I run the test script in the command line using node.

                  https://github.com/bnielsen1965/rpi-gpio-buttons

                  By the way, this learning nodejs and module development is not all that easy.

                  kayakbabeK 1 Reply Last reply Reply Quote 1
                  • kayakbabeK Offline
                    kayakbabe @kayakbabe
                    last edited by

                    @kayakbabe DRAT! I just crushed my microSD with my wheelchair. It is definitely time for bed.

                    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