Calling a function in LC Server

Are you using LiveCode to create server scripts or CGIs?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

fritzdekatt
Posts: 57
Joined: Fri Feb 09, 2018 2:28 am

Calling a function in LC Server

Post by fritzdekatt » Tue Feb 12, 2019 4:54 am

If I'm building a browser interface to LC Server, assuming we have no access to any LC desktop objects, what do I use to call a function? I could use a conditional based on the Get array or post via a form, but if I wanted to use a button or list item to call javascript, how could I make this trigger a function? Or is there a more LC-centric alternative?

Also, in PHP or other web languages, I'd test for the existence of a $_POST variable before using it (like after posting a form or populating text fields for an update request). In LC, it doesn't seem to matter, if I reference an item from the $_POST array, whether it exists or not. I get no error. Trying to use the "exists" function doesn't work as expected. How do you test for existence of a variable (or an array item)? I can do a conditional based on a value within, but that seems academic if I don't know it exists yet.

If anybody knows of any tutorial or examples addressing these questions, I'd love to know about it. I heard somebody actually designed a CMS on LC Server but, though allusions to this abound, I've yet to see it. That would be a great source of information if one was good enough to share the details.

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Calling a function in LC Server

Post by ClipArtGuy » Tue Feb 12, 2019 5:40 am

I'm pretty sure the CMS you're looking for is RevIgniter. https://revigniter.com/

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Calling a function in LC Server

Post by FourthWorld » Tue Feb 12, 2019 6:18 am

Have you seen these lessons?
http://lessons.livecode.com/m/4070

As for $_POST, what code are you using that you would expect to see an error from?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

fritzdekatt
Posts: 57
Joined: Fri Feb 09, 2018 2:28 am

Re: Calling a function in LC Server

Post by fritzdekatt » Tue Feb 12, 2019 10:47 am

ClipArtGuy wrote:
Tue Feb 12, 2019 5:40 am
I'm pretty sure the CMS you're looking for is RevIgniter. https://revigniter.com/
Thank you. I've seen this. As far as I can see, it's an MVC framework for LC, like CodeIgniter is an MVC framework for PHP. I'm sure this is useful for 99 percent of LC server coders, but I don't care for MVC frameworks, either in PHP or Node. Implicit in their use is that you already pretty much know how to code in the language they're based on. I didn't like CodeIgniter, or Laravel, or CakePHP. Since there are 11,328 books on PHP, and you can't swing a dead cat on YouTube without hitting 90 PHP tutorials, I was able to learn PHP just fine. It is my contention that it is lack of documentation and complete examples, not terminal stupidity, that prevents my simply using LC out of the box to code the server, though no doubt that is a component in my lack of understanding.

If you're suggesting that the RevIgniter site is itself a CMS of sorts and can be studied, that would be fine if I had some code (as long as he didn't write his site in RevIgniter). Again, I am certain this is a fine product and his site is beautiful (Table of Contents access is somewhat complex but perfectly understandable) but I don't want a framework. Just a roadmap of the bare LC server.

I'm glad you mentioned it, because I bet lots of other people would love RevIgniter (as I might someday when I pick up a bit more of how the server works). Regardless, I appreciate your response.

fritzdekatt
Posts: 57
Joined: Fri Feb 09, 2018 2:28 am

Re: Calling a function in LC Server

Post by fritzdekatt » Tue Feb 12, 2019 11:03 am

FourthWorld wrote:
Tue Feb 12, 2019 6:18 am
Have you seen these lessons?
http://lessons.livecode.com/m/4070

As for $_POST, what code are you using that you would expect to see an error from?
I have seen these lessons many times, thank you. I can send and receive data from the server. What I can't do is call one of my user-defined functions on the same page (or from an include). I can post to another page to run a function but I figured there would be an LC specified way to explicitly call a function like we can in a desktop scenario. I assume this might be done with a javascript enabled button. I can create a Javascript button, but I don't seem to be able to call an LC function yet.

When I reference $_POST["FirstName"] at the top of a PHP page, and there is no such post variable, I will get an error. When I do the same with LC, I do not. Is that how it's supposed to work? I tried an

If exists($_POST["FirstName"]) then

but couldn't get a response. What's the code to test for existence on the server? I've put the post variable into a regular variable and tested that, but same outcome.

Granted that my problems are in large part due to my unfamiliarity with LC in general, I am learning LC in the context of a cloud-based solution so it strikes me as reasonable to pursue both the client and server ends more or less simultaneously, though I may be deluding myself there.

Thank you for your consideration.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Calling a function in LC Server

Post by bogs » Tue Feb 12, 2019 12:44 pm

I don't know if this pertains to your question or not, but have you seen this lesson from Jacque's site?
http://www.hyperactivesw.com wrote: Introduction to LiveCode CGIs
CGIs provide dynamic content to web sites, and allow flexible delivery of information. This tutorial explains how to use LiveCode as a CGI engine on your server, and walks you through several CGI examples written in LiveCode's easy programming language.
Someone else also had information on Lc server, but I think that was more basic. If I remember it, I'll stop in with the link.
Image

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: Calling a function in LC Server

Post by ghettocottage » Wed Feb 13, 2019 3:40 am

fritzdekatt wrote:
Tue Feb 12, 2019 4:54 am
If I'm building a browser interface to LC Server, assuming we have no access to any LC desktop objects, what do I use to call a function? I could use a conditional based on the Get array or post via a form, but if I wanted to use a button or list item to call javascript, how could I make this trigger a function? Or is there a more LC-centric alternative?
Can you elaborate a little more on what you would like to do with this?

Are you trying to trigger a Livecode function with javascript? I think you would want to use jQuery for something like that, similar to how you would in PHP, since PHP and Livecode Server are both server-side.

Here is an Ajax/Livecode Server tutorial that might shed some light:
http://lessons.livecode.com/m/4070/l/14 ... ode-server

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: Calling a function in LC Server

Post by SparkOut » Wed Feb 13, 2019 6:15 am

Back to one of your basic questions, LiveCode is typeless and intuitive in variable definition, so there is no "exists" you can test, the act of checking a variable name will create it, initialised to empty

Code: Select all

if $_POST["FirstName"] is not empty then
 put myFunction($_POST["FirstName"]) into tNewVar
end if 
will only create tNewVar if the server has received content in the FirstName value posted. You should still validate the contents of that value before doing anything with it, of course.

fritzdekatt
Posts: 57
Joined: Fri Feb 09, 2018 2:28 am

Re: Calling a function in LC Server

Post by fritzdekatt » Thu Feb 14, 2019 3:15 am

bogs wrote:
Tue Feb 12, 2019 12:44 pm
I don't know if this pertains to your question or not, but have you seen this lesson from Jacque's site?
http://www.hyperactivesw.com wrote: Introduction to LiveCode CGIs
CGIs provide dynamic content to web sites, and allow flexible delivery of information. This tutorial explains how to use LiveCode as a CGI engine on your server, and walks you through several CGI examples written in LiveCode's easy programming language.
Bogs
Oddly enough, this link addresses how to use stacks on the server, which is a question I haven't explicitly asked yet, since there doesn't seem to be any analogue to "stacks" in any other web language I know.
This is a useful link indeed.

A lot of the rest of the questions I have are being addressed just by blindly attempting to swap old php code with what I think would work in LiveCode. It's not pretty, but it works just often enough to be useful.
Thank you as always.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Calling a function in LC Server

Post by bogs » Thu Feb 14, 2019 7:01 am

Glad it was helpful, Jacque probably has tricks regarding this stuff going back a long ways. If I can get the other brain cell to fire and remember where I saw the other possibly relevant articles, I'll throw them up and you can see what sticks :wink:
Image

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Calling a function in LC Server

Post by bogs » Thu Feb 14, 2019 2:57 pm

As I was going through my links, I did come across this site, which appears to have a fairly cohesive section on Lc Server related tasks -
Selection_004.png
How many we serve you ?
Image

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Calling a function in LC Server

Post by FourthWorld » Thu Feb 14, 2019 7:56 pm

There's a nearly infinite range of things we could convey about using LiveCode on servers, so for the moment I'm most interested in addressing the specific questions asked and then we'll see where things lead once those are out of the way.

fritzdekatt, if I read your opening post here correctly it offers two questions:

1. How do I call a function?
2. How do I test whether an array element contains a value?

The latter seems to have been handled well by SparkOut, who noted that the way we test variables like array elements for a value is to see if they're empty.

"Calling a function" can mean different things, so I'll provide a little background which will lead to a question, the answer to which will guide us to provide exactly what you're looking for:

LiveCode shares an uncommon trait with Pascal and a few others, distinguishing between functions and commands. Most languages (C, JavaScript, etc.) provide only functions, so this distinction may not be readily apparent.

In LiveCode, commands are most commonly used to perform actions, and function for obtaining values. The first word of very LiveCode statement is a command, and a statement may or may not include function calls.

So if you ask, "How do I call a function?" in the sense of obtaining a value, the answer is syntactically different from the more general "How do I invoke a specified response from the server?"

If you're already able to call custom commands on your server, we can explore the nuances of functions.

But if your query is more general, looking to pass params to a server script to obtain different results, our focus would be different.

I'll venture that your query is of the latter type, yes?

If so, how would you do that in PHP? Let's build on what you know to find the most powerful onboarding for your LiveCode mastery.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

fritzdekatt
Posts: 57
Joined: Fri Feb 09, 2018 2:28 am

Re: Calling a function in LC Server

Post by fritzdekatt » Tue Feb 19, 2019 12:08 am

SparkOut wrote:
Wed Feb 13, 2019 6:15 am
Back to one of your basic questions, LiveCode is typeless and intuitive in variable definition, so there is no "exists" you can test, the act of checking a variable name will create it, initialised to empty

Code: Select all

if $_POST["FirstName"] is not empty then
 put myFunction($_POST["FirstName"]) into tNewVar
end if 
will only create tNewVar if the server has received content in the FirstName value posted. You should still validate the contents of that value before doing anything with it, of course.
That's exactly what I wanted to know. It baffled me that I wasn't getting errors using these variables when they were based on conditionals that under no circumstances generated an error, as they would in PHP (or Node or any other server side script I know of). Now that I know this, it's very useful. Without being aware of this, it's an astounding pain. Thanks very much. You have enormously simplified the rest of my month.

fritzdekatt
Posts: 57
Joined: Fri Feb 09, 2018 2:28 am

Re: Calling a function in LC Server

Post by fritzdekatt » Tue Feb 19, 2019 12:27 am

FourthWorld wrote:
Thu Feb 14, 2019 7:56 pm
There's a nearly infinite range of things we could convey about using LiveCode on servers, so for the moment I'm most interested in addressing the specific questions asked and then we'll see where things lead once those are out of the way.

fritzdekatt, if I read your opening post here correctly it offers two questions:

1. How do I call a function?
2. How do I test whether an array element contains a value?

The latter seems to have been handled well by SparkOut, who noted that the way we test variables like array elements for a value is to see if they're empty.

"Calling a function" can mean different things, so I'll provide a little background which will lead to a question, the answer to which will guide us to provide exactly what you're looking for:

LiveCode shares an uncommon trait with Pascal and a few others, distinguishing between functions and commands. Most languages (C, JavaScript, etc.) provide only functions, so this distinction may not be readily apparent.

In LiveCode, commands are most commonly used to perform actions, and function for obtaining values. The first word of very LiveCode statement is a command, and a statement may or may not include function calls.

So if you ask, "How do I call a function?" in the sense of obtaining a value, the answer is syntactically different from the more general "How do I invoke a specified response from the server?"

If you're already able to call custom commands on your server, we can explore the nuances of functions.

But if your query is more general, looking to pass params to a server script to obtain different results, our focus would be different.

I'll venture that your query is of the latter type, yes?

If so, how would you do that in PHP? Let's build on what you know to find the most powerful onboarding for your LiveCode mastery.
RE: Calling functions
Indeed, I ended up just translating directly from PHP to call my functions, as you suggested, which works fine except for some cases of AJAX, which I have trouble doing in PHP also.

I was mislead (or mislead myself) by a statement that one could call stacks from the server, which lead me into speculations that this gave a web services client potentially application level access to a server process, like some C# clients do (by virtue of the fact they're running the same assemblies in .NET). This turns out to be a flawed assumption. I was working an example on LC's Cloud and Database video series and it seemed to be showing me something that wasn't really there. It was truly just a web services example--as it plainly said--but the service was delivered through a desktop client. My simple brain found this confusing, even though I'd seen this before in other scripting environments (and found it confusing on those occasions also).

RE: Testing for Existence
SparkOut did indeed solve my "EXISTS" problem in one fell swoop. Thanks to him, I can go back to watching basketball and sunbathing on my deck without even thinking about why my conditional $_POST[] is not freaking out the web server.

Thank you for your continued assistance.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Calling a function in LC Server

Post by FourthWorld » Tue Feb 19, 2019 7:12 pm

fritzdekatt wrote:
Tue Feb 19, 2019 12:27 am
RE: Calling functions
Indeed, I ended up just translating directly from PHP to call my functions, as you suggested, which works fine except for some cases of AJAX, which I have trouble doing in PHP also.
Was the difficulty on the AJAX side or the PHP side? I've used XMLHttpRequest to call LC Server routines, so I may be able to help once I learn more about where the trouble lies.
I was mislead (or mislead myself) by a statement that one could call stacks from the server, which lead me into speculations that this gave a web services client potentially application level access to a server process, like some C# clients do (by virtue of the fact they're running the same assemblies in .NET). This turns out to be a flawed assumption. I was working an example on LC's Cloud and Database video series and it seemed to be showing me something that wasn't really there. It was truly just a web services example--as it plainly said--but the service was delivered through a desktop client. My simple brain found this confusing, even though I'd seen this before in other scripting environments (and found it confusing on those occasions also).
LC has very good socket support, with async options similar in some respects to Node.js' single-threaded message-based operations. Most commonly used in clients, LC's flexible socket commands can also lend themselves well to working behind a reverse proxy on a server, such as Node.js or NgineX, or for lighter loads even take on the role of handling external network I/O directly (I once wrote an HTTPd script as an exercise and was pleased with the results, though not surprisingly Apache outperformed it in my tests by more than double; still a worthy use of time, as for certain microservices a completely LC-based solution may offer some development advantages).

So while LC Server is designed to work optimally with CGI, the full range of things that can be done for client-server tasks with sockets is quite broad. And now that we have the ability to specify UDP ports many P2P options are available as well.

I'm often tempted to write a book-long post about all the things one could do in this area - client-server is a big part of my work, and I'm prone to over-enthusiasm. :)

So instead, given how many ways there are to do so many things with LC, let's start first with the most important one: What would you like to do?

For example, since the notion of "calling a stack on a server" captured your attention, what would that look like in terms of the user experience you're envisioning?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “CGIs and the Server”