• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
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.

Synchronus socketNotification ?

Scheduled Pinned Locked Moved Troubleshooting
5 Posts 2 Posters 2.2k 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.
  • R Offline
    romain
    last edited by Jun 20, 2017, 3:13 PM

    Is there a way to have a synchronus socket notification or something similar ?
    Let me explain, I have a module, that module have a node helper. I can send thing directly to the node_helper with a third party software. And I do it by making a GET. As an example, One of the get is about retreving a variable in previously set in config map. But to get that I have to send a socket notification and wait until I have the answer. But this is asynchronus so it doesn’t wait so I can’t send the answer back.
    I though about sending the config to the node_helper in the start function but it’s kind of a pain because I would have to update it every so often if the value change.

    So is there a way to do some sort of synchronus request from the node_helper to the associate js file ?

    1 Reply Last reply Reply Quote 0
    • S Offline
      shbatm Module Developer
      last edited by Jun 20, 2017, 6:27 PM

      Can’t you just send a socketNotification back to the associated js file when the information is received and call a function? Both node_helper and the module.js file have both socketNotificationReceived: function(notification, payload) and sendSocketNotification(notification, payload) methods.

      R 1 Reply Last reply Jun 21, 2017, 1:09 PM Reply Quote 0
      • R Offline
        romain @shbatm
        last edited by Jun 21, 2017, 1:09 PM

        @shbatm I could but then It would be asynchronus. I can"t have a function doing stuff, get the variable I want, continue to do stuff.
        I would have to do stuff, send a socket notification to request the variable, receive the notificataion, send back the variable with another notification then get the notification and only then I would be able to continue what I was doing. This is a very slow process just to get a variable value. Also I would have to store a lot of variable so I can use them in two different function.

        What I want is similair to a function call you know ? doing stuff in the row. I don’t want to do stuff in two different fonction asynchronusly.
        It can be usefull to do it like this in some cases don’t get me wrong, but’s it’s create somes issue as well in some other cases

        1 Reply Last reply Reply Quote 0
        • S Offline
          shbatm Module Developer
          last edited by shbatm Jun 21, 2017, 3:45 PM Jun 21, 2017, 3:40 PM

          I’m not sure how you would block JavaScript to actually make it synchronous since it is an asynchronous language. I use the “circular” method I described in one of my modules with very little lag; yes you would have to break it up into two functions for part A and part B.

          Is the GET you are mentioning just getting a config value from the module’s config? If so, why not just pass the config value you need with a module.js->node_helper socket notification and have the node_helper store the value until you need it? Any time it needs to be updated, just resend the notification; that way node_helper always has the latest value available.

          node_helper.js:

          module.exports = NodeHelper.create({
              config: {},
              socketNotificationReceived: function(notification, payload) {
                  if (notification === 'CONFIG') {
                      this.config = payload;
                  }
              },
              yourFunction: function(variables) {
                  configValueNeeded = this.config.valueNeeded;
                  ... // do something
              },
              ...
          });
          

          module.js:

          start: function() {
              this.updateConfig();
          },
          
          updateConfig: function() {
              this.sendSocketNotification('CONFIG', this.config);
          },
          
          someFunction: function() {
              // config changed for some reason
              this.updateConfig();
          },
          

          If you’re getting a config value from some third-party service: in your node_helper you can use the request module:

          getData: function() {
              // DO STUFF
              var request = require('request');
              request({
                  url: apiUrl,
                  method: 'GET',
              }, (error, response, body) => {
                  if (!error && response.statusCode == 200) {
                  // CONTINUE DOING YOUR STUFF
                  } else if (response.statusCode === 401) {
                      console.error(this.name, error);
                  } else {
                      console.error(this.name, "Could not load data.");
                  }
              });
          },
          
          1 Reply Last reply Reply Quote 0
          • R Offline
            romain
            last edited by Jun 28, 2017, 9:04 AM

            Right, I think this is the only way too. It’s kind of annoying though because the value can change so I would need to send it to send node helper every single time it change to keep it up to date.
            Oh well.

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            1 / 1
            • First post
              1/5
              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