Read the statement by Michael Teeuw here.
when to use init , start, loaded of node helper
-
I’m learning to write my own module and poking around on this forum. I am not sure about when to put code in the init, start, or loaded functions of the node helper. I’m basically starting with the SampleModule by Sam. I’ve got my script working from the operating system running it with “node myscript.js” and it even uses a node package (was really proud of myself over that). And now I want to make it into a module for MagicMirror.
I’m wondering what and when to put code in init, start or loaded sections of the node_helper example?
I’ve look at the modules repository for examples and it seem like its all over the place.
Kelly
P.S. I’m very new to node.js and this is my first from scratch attempt to do anything with it at all. I feel like I’m halfway there. But not I"m stuck.
-
You can ignore
.init()
or.loaded()
. Usually, you don’t need to touch these methods ofnodeHelper 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
andsocketIO
), 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.
-
@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
-
@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?
-
@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())
-
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… -
@kayakbabe think of the helper as a web service provider
gets a request (notification)
process
send response(notification) -
@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.
-
@kayakbabe DRAT! I just crushed my microSD with my wheelchair. It is definitely time for bed.