Function serverside

Started by mirko.ugolini, March 21, 2018, 03:00:57 AM

mirko.ugolini

Hello Felix,

First let me thanks you for your wonderfull project.
I like it a lot and I trying to use it for my Domotic system.

But I ave one question regarding the metrics.js file definitions.
In the  Metrics.js there are definition of function that must be executede server side that are passed tu the Client UI.
is it not possible to define the name of the funciont to be executed and  run them server site in the Gateway.js?

for example:

serverExecute:function(node){
                            var targetNow=0, modeNow='';
                            if (node.metrics['MODE']) modeNow = node.metrics['MODE'].value;
                            if (modeNow=='AUTO') return;
                            exports.tstatRequest({ 'tmode':3 }, node._id);
}


I read something in this article: https://stackoverflow.com/questions/8206453/call-function-by-string-name-on-node-js

Please let me know

Regards

Mirko  Ugolini

Felix

I appreciate the feedback,
I am not fully understanding your question.
There are functions that can be executed at the server side when something happens (ex send an email or SMS or a message to a node).
For the client side - I implemented a socket message system to avoid functions. For instance this can be used to play a sound in the browser at the client side when something happens - look for this code:
io.sockets.emit('PLAYSOUND', 'sounds/alert.wav');


There are also evaluated showCondition functions which are defined at the server and evaluated/executed at the client browser. These functions that need to be passed to the UI (browser) need to be packaged as strings, then evaluated in the browser as executable code. But there are no functions that you can randomly execute at the client. However you could implement something similar to showCondition and add your own custom code to execute it when something happens, if that's what you are asking.

Anyway on the server side the function to execute when something happens is simply called serverExecute and is called if it is defined/not null.
This is also explained in the comments in metrics.js.

I hope it helps you.

mirko.ugolini

#2
Good morning Felix,

Thanks for the reply.

I apologize for my poor English but maybe I had not explained my request well.

I have prepared an attached file with more detailed explanations

var Handler={};

Handler.application_run = function (name) {
console.log(name)
}

Now call it like this

var somefunc = "application_run";
Handler[somefunc]('jerry codes');
Output: jerry codes

2) If you want to keep methods in a different js file
//    Handler.js
module.exports={
    //  name_exported : internal_name
    application_run = function (name) {
console.log(name)
}
}



:)

Greetings

Felix

OK. I get it now :)

So you just want to reference functions defined elsewhere.
For calling a function defined elsewhere - that I am not sure but you should be able to reference it as long as you resolve any scoping for the function you define. This is more a javascript coding question rather than a lack of a feature.

I agree it's not ideal to define the functions inline where the metric events are defined, but since these functions are unique and this is common practice in javascript, I prefer this method for immediate visibility. It's simply my preference and not something very unusual. You can do it differently if you want to.

There is 1 misunderstanding in your assumptions: you cannot pass a function to the client (there is no automatic serialization/deserialization for functions).
So even if that function is defined on the server side, it will not go to the client because it cannot be serialized.
And I'm not sure about the connection of this question to the previous.

mirko.ugolini

#4
Hello Felix,

thank you for the clarification.

Regarding this point:

Quote from: Felix on March 22, 2018, 09:01:09 AM
There is 1 misunderstanding in your assumptions: you cannot pass a function to the client (there is no automatic serialization/deserialization for functions).
So even if that function is defined on the server side, it will not go to the client because it cannot be serialized.
And I'm not sure about the connection of this question to the previous.

I did not want to pass a function to the client and serialize it, I simply wondered if it was possible to avoid defining the functions that must be performed serverside in a block of code that in a certain sense is sent to the client in the definition of specific events for that node.

Regards

Mirko


Felix

Quote from: mirko.ugolini on March 22, 2018, 11:47:32 AM
I did not want to pass a function to the client and serialize it, I simply wondered if it was possible to avoid defining the functions that must be performed serverside in a block of code that in a certain sense is sent to the client in the definition of specific events for that node.
In javascript, whether you define a function and reference it (like a variable) or define it inline when you assign it to an object's property, it's basically the same thing. That object property will still point to a function.