Read the statement by Michael Teeuw here.
Simplfying my understanding of sendNotification and sendSocketNotification with and without a this
-
I think we have five ways to use when sending notifications around in MM. I’d like to know if I finally have a grasp on how it works. Can someone confirm if I’ve got this right?
In my mental analogy I kind of imagine a node_helper is like a child of the mother module which spawned it.
So when I need to send information around inside MagicMirror this is how I think it works:
1. my MotherModule can send info to it's own kid node_helper with sendSocketNotification 2. my MotherModule can send info to the MM in this [edited: browser instance] with sendNotification 3. my node_helper kid can send to it's MotherModule with this.sendSocketNotification 4. my node_helper kid to all it's cloned MotherModule instances, but no module outside the immediate family with sendSocketNotification (ie maybe I have four instances of the same module just using diff header names and setup with four diff config variables to kickstart them. I better be careful and keep track of which Mom send what info that the kid is replying to, or all the Mom's in the family will know the kid's secrets.) 5. can my kid node_helper sned info to the whole system? not directly possible. The kid node_helper has to send info to the MotherModule and then MotherModule will be able send it to the [edited: browser instance] .
“this.” makes it a private conversation between the specific instance of the Mother Module and the node_helper . Not having “this.” make it more public. In the case of the node_helper, it send to every instance of the Mother Module if there is more than one in use; and, in the case of the MotherModule, it blasts the info out into the entire MagicMirror.
-
@kayakbabe
here is a drawing I made years ago, the right side is clipped on purpose as it it is just more modules doing the samethe socketNotification (send and receive) are between the module and its node helper ONLY. (1/3/4)
IF there are multiple clients (browsers) connected and running the same module, when the node helper sends the message it goes to ALL connected clients.IF you want to support MULTIPLE instances, then you should send some unique ID (this.identifier) from the module to helper on each sendSocketNotification from the node_helper(which has to include that ID) , AND CHECK in the socketNotificationReceived if the ID matches…
NOTE. for multiple browsers on MULTIPLE machines, the this.identifier will be the same (as its the logical count from top of config.js )
I provide a small module (getip) that can return the clients IP address(as seen by node_helper) so you can use this as the unique ID insteadthe send/receive notifications are IN THE SAME browser.
send is a BROADCAST that goes to every module in this browser.
(note: if you start another browser instance, connecting to the SAME MagicMirror, the broadcast is ONLY to ONE browser, where the module initiated connection)the ‘this’ is because the Module is object oriented, and this points to the individual context for the MagicMirror provided functions (notifications, getDom(), updateDom()… your #2
you MUST use this. for these functions#5, correct…
(as an example see my https://github.com/sdetweil/MMM-CurlToNotification,
which accepts an http post(at the server), and forwards that to module(unknown locations) to broadcast as notification) -
@sdetweil AHHHH!!! AHA moment here. That graphic really helps me understand.
Not everyone runs a single mirror on it’s own computer like I do. Some use the MM like digital signage. One beefy server doing the heavy lifting and then a bunch of lightweight microcomputers just displaying the MM front end.
The Kid node_helper is running on the server! While the MotherModules are only in their own browsers on whatever weenie machine is displaying the front end of MM. (might not be weenie but might be.)
so I see how I would have to be really careful about unique id’s in an environment where I run the MagicMirror on a server and have a bunch of displays all connected to it. May not want the KidModule blasting the same info out to every single display. Unique ID’s mean info would only go to the display it’s intended for.
Though I supposed it’s possible that one would want the same info to go out to every display.
I was thinking of setting up a few displays showing golf tournament scores and in that situation, I’d want every single mirror to show the same info. So in this use case, I wouldn’t want the unique id at all.
-
@kayakbabe said in Simplfying my understanding of sendNotification and sendSocketNotification with and without a this:
I’d want every single mirror to show the same info. So in this use case, I wouldn’t want the unique id at all.
yes, implementation architecture and design matter, (like always)!
because the MagicMirror instances are pulling the same html file and config
they are ordered the same… you would need something (getip) to provide a more certain unique ID when you need that
In the case of multiple instances in multiple clients you would need both ip and identifier in the ID. -
@kayakbabe said in Simplfying my understanding of sendNotification and sendSocketNotification with and without a this:
May not want the KidModule blasting the same info out to every single display. Unique ID’s mean info would only go to the display it’s intended for.
NO… the kidmodule will ALWAYS SEND (BLAST) to ALL connected…
it is using the socketio room technologyits the connected module that is responsible for checking if the received notification is intended to go to it…
-
and on the architecture/design topic
if you are doing the signage solution, then one would
want one data fetch and broadcast to each client
vs each client getting the data on their ownsome provider apikeys count data access requests (openweather) . so while you dont NEED a node_helper, sometimes you want it. but some of the existing modules dont really support that thought