Read the statement by Michael Teeuw here.
Synchronus socketNotification ?
-
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 ?
-
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)
andsendSocketNotification(notification, payload)
methods. -
@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 -
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."); } }); },
-
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.