Server Questions

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

A1Qicks
Posts: 79
Joined: Sat Dec 26, 2015 10:47 am

Server Questions

Post by A1Qicks » Thu Dec 31, 2015 6:33 pm

So I'm making a chat app with LiveCode connecting to a MySQL database, and I've got the very basics functional. It updates the database, it queries it, it shows info from it.

What I'm wondering is how to organise regular queries. If it's going to post new messages to the chatboard in real time, I'd imagine it needs to be querying the database every second or so. But considering it has to open and close the connection every time it does that (or else leave the connection open, which I've been advised against) it seems like it'd be doing that very regularly. Is this normal? Is there a way around it?

Likewise, on the LiveCode end, should this simply be a "send [query command] to me in 1 second" in the querying loop so it does it every second or is there a better way of doing this? I feel like I've seen advice against using that but don't know what would be better.

Long story short, I know how I can make it work, I just want to know the best way to make it work.
Last edited by A1Qicks on Sun Jan 03, 2016 1:16 pm, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Database Etiquette and Timed Loops

Post by FourthWorld » Thu Dec 31, 2015 8:02 pm

What is the role of the database in this chat app?

Here a Lesson on making a chat app in LiveCode:
http://lessons.livecode.com/m/4071/l/12 ... ng-sockets
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

A1Qicks
Posts: 79
Joined: Sat Dec 26, 2015 10:47 am

Re: Database Etiquette and Timed Loops

Post by A1Qicks » Thu Dec 31, 2015 8:18 pm

The database has thus far taken the place of a server for the chat app. It was a recommendation made to me by someone else - what the advantages of a server over a database?

Looking at the general principle of the article, I can see how it might rephrase a lot and take out the MySQL element, and it takes out the need to keep looping a database query, I think, but what the extent of the importance of that?

A1Qicks
Posts: 79
Joined: Sat Dec 26, 2015 10:47 am

Server Questions

Post by A1Qicks » Sun Jan 03, 2016 12:41 am

Since there's no answer to that, I'll ask a couple of peripherals following the article provided:

1.
on socketClosed pSocket
delete line lineoffset(pSocket, sConnectedClients) of sConnectedClients
end socketClosed

Why is 'lineoffset' required? I'm just about following the concept of the code itself, but I don't understand why it can't just be "line pSocket of sConnectedClients". It would make sense that it would need the offset if LiveCode used it as a way to locate the line pSocket, but since it's an offset between the line pSocket (and therefore it has to find it anyway) and sConnectedClients, why is it "lineoffset(pSocket, sConnectedClients" of sConnectedClients" instead of "line pSocket of sConnectedClients"?

2.
Wider question phrased in the form of this article:

on broadcastServerClientConnected pSocket
put pSocket & return after sConnectedClients
read from socket pSocket until return \
with message "broadcastServerMessageReceived"
end broadcastServerClientConnected

Where does pSocket come from? It's not a variable defined outside of the handlers nor a temporary one within the handlers; it corresponds certainly to each individual client (pSocket is a variable referring to whichever client is in use at any given time, yes?) but how does LiveCode automatically know that "on broadcastServerClientConnected pSocket" refers to the client sending the message?

I presume the answer lies in:

command broadcastServerStart
if not sRunning then
put true into sRunning
accept connections on port kPort \
with message "broadcastServerClientConnected"
end if
end broadcastServerStart

So the client connects and the connection causes the message "broadcastServerClientConnected" to be sent (presumably by the host to the host - or from the socket to the host? It doesn't specify and I can feel the coffee draining out of my brain fluid), and then broadcastServerClientConnected as a handler acts upon pSocket, but how does LiveCode know that pSocket refers to the client which prompted the message?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Server Questions

Post by FourthWorld » Tue Jan 05, 2016 7:17 pm

Busy holiday; I'd hoped someone else might pick up this thread, but I'll see what I can do here:
A1Qicks wrote:what the advantages of a server over a database?
A database is a means of storing and retrieving large amounts of data, and a server is a means of making centralized resources available to clients.

In some cases a database may be a database server in which is can accept requests and deliver data to clients without needing other software. For example MySQL, MongoDB and CouchDB are database servers, but SQLite is not.

That said, there are many kinds of servers for different tasks, and as servers go a database server is fairly limited in what it can do. Mostly it just stores and retrieves data, and broadcasting is beyond the scope of most DB servers.

When you only need to store and retrieve data among a relatively small number of trusted users, and when the database is only being used on a local network and not exposed to the Internet, you can use it directly for the types of tasks it's designed for, which even include use account management and authentication.

But if you need it to do anything more, or you need to protect it from being exposed to the Internet, most commonly a middleware is used, such as PHP, Python, Perl, or LiveCode, which sits between a Web server such as Apache and calls the database only within the server. So the user is making Web calls, Apache hands those off to the middleware, and the middleware deals with the database using sockets internal to the server. This protects the server from being exposed to the Internet directly, and allows greater functionality provided through the more general-purpose middleware.

Many chat services are implemented using HTTP, using the middleware to coordinate data with the clients. But as you found, in many systems this requires the client to poll the server, or use WebSockets or other newer options to receive notification of new messages.

When using a middleware tool like PHP or LiveCode it may not even be necessary to use a database to store the messages. In many cases the individual messages are of little value as discrete elements, equally useful if included as entries in a single log file such as we have with IRC chats.

So if you needed to make an HTTP-based Web chat service, the database is purely optional, and may be just exta overhead.

But you can make an even leaner system that uses sockets directly, provided your server allows you to run custom server software beyond Apache. A dedicated server or VPS will allow that, but a shared host won't, since it would be possible for a custom server application to affect other users sharing the machine.

If you're using a dedicated server or VPS, having a custom app like the Chat Server from the example I'd linked to is a very efficient solution, making full use of socket messages without needing to poll.

If you're using a shared hosting service you may have no choice but to use an HTTP-based solution, but in that case I'd check with the service to see if they have one pre-installed for use, which will usually be much simpler than making one from scratch - phpFreeChat is one example:
http://www.phpfreechat.net/

Even simpler, some chat software is available as a service you can plug into your Web site with a quick copy-and-paste of some JavaScript. The PHPFreeChat page has links to such services, and there are many others around.


A1Qicks wrote:1.
on socketClosed pSocket
delete line lineoffset(pSocket, sConnectedClients) of sConnectedClients
end socketClosed

Why is 'lineoffset' required? I'm just about following the concept of the code itself, but I don't understand why it can't just be "line pSocket of sConnectedClients". It would make sense that it would need the offset if LiveCode used it as a way to locate the line pSocket, but since it's an offset between the line pSocket (and therefore it has to find it anyway) and sConnectedClients, why is it "lineoffset(pSocket, sConnectedClients" of sConnectedClients" instead of "line pSocket of sConnectedClients"?
Short answer: because that's how it works. :)

Long answer:
The lineOffset function returns the number of the found line, which can then be used with get, put, delete, or other commands to act on that line.

Your suggestion that it would be useful to have something that would both find a given line and act on it chimes with my own preferences, but this was explored on the use-livecode list a while back and if you read the whole discussion you'll find some of the reasons this is not as trivial as it might seem:
http://runtime-revolution.278305.n4.nab ... 98214.html

Perhaps better would be to use an array rather than a list. Array access is generally faster to get and set values in than delimited text, and the syntax is some cases more compact, e.g.:

Code: Select all

delete variable sConnectedClients[pSocket]
2.
Wider question phrased in the form of this article:

on broadcastServerClientConnected pSocket
put pSocket & return after sConnectedClients
read from socket pSocket until return \
with message "broadcastServerMessageReceived"
end broadcastServerClientConnected

Where does pSocket come from? It's not a variable defined outside of the handlers nor a temporary one within the handlers; it corresponds certainly to each individual client (pSocket is a variable referring to whichever client is in use at any given time, yes?) but how does LiveCode automatically know that "on broadcastServerClientConnected pSocket" refers to the client sending the message?

I presume the answer lies in:

command broadcastServerStart
if not sRunning then
put true into sRunning
accept connections on port kPort \
with message "broadcastServerClientConnected"
end if
end broadcastServerStart

So the client connects and the connection causes the message "broadcastServerClientConnected" to be sent (presumably by the host to the host - or from the socket to the host? It doesn't specify and I can feel the coffee draining out of my brain fluid), and then broadcastServerClientConnected as a handler acts upon pSocket, but how does LiveCode know that pSocket refers to the client which prompted the message?
Short answer: because that's how it works. :)

Long answer:
Socket communications keep track of which socket and port are being affected (most often in the form <socket>:<port>, e.g. "100.100.100.100:80"), usually passed to the socket-related messages as an argument. Your hunch is confirmed by the Dictionary entry for the accept command, where it notes:
The callbackMessage is sent to the object whose script contains the accept command. Either one or two parameters are sent with this message. The first parameter is the IP address of the system or process making the connection. If a datagram is being accepted, the second parameter is the contents of the datagram.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

A1Qicks
Posts: 79
Joined: Sat Dec 26, 2015 10:47 am

Re: Server Questions

Post by A1Qicks » Tue Jan 05, 2016 11:31 pm

Thanks so much for the detailed answer! It explains a lot and gives me a lot more clarity for what I'm working with.

The reason I currently prefer a database to a server is that I have access to a MySQL database from other webhosting, allowing me to test stuff out - while my internet plan isn't designed to accommodate a server.

One thing I still don't understand about servers, though - the tutorial about them refers to ports and sending messages and whatnot, but not the actual location of the server. I tried making both server and client as far as the instructions guided me, but for a complete beginner they're not overly clear about what should happen or where to put anything.

Creating both host and client within Livecode prompted no direct success when trying to get it to do anything - I'd imagine it's not connecting to anything, but I got sufficiently lost that I didn't even know what question to ask. That's another reason I've been working with databases - the LiveCode guide to MySQL interaction made a lot more sense to me!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Server Questions

Post by FourthWorld » Tue Jan 05, 2016 11:52 pm

This may be more inspiring: in the LC IDE, click "Resources" in the toolbar, and in the Resource Center window that opens scroll down to "Sample Projects" to find the "Internet Chat" project. You can open them directly from there, and they're already set up to use localhost so you can see them in action immediately.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

A1Qicks
Posts: 79
Joined: Sat Dec 26, 2015 10:47 am

Re: Server Questions

Post by A1Qicks » Tue Jan 05, 2016 11:58 pm

Thanks! I'll give that a try!

(Although currently it seems to be non-responsive... The struggles I get from using the latest LiveCode version. May have to nip back to a previous one, the dictionary isn't launching either)

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Server Questions

Post by FourthWorld » Wed Jan 06, 2016 1:12 am

Which version are you using? In general it's best to use the latest Stable build, and leave any "DP" releases for experienced devs to test.

Currently the latest Stable is 7.1.1:
http://downloads.livecode.com/livecode/
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

A1Qicks
Posts: 79
Joined: Sat Dec 26, 2015 10:47 am

Re: Server Questions

Post by A1Qicks » Wed Jan 06, 2016 2:23 pm

Been using 8 for the same reason I went Windows Insider for Windows 10 - I like having the latest tools. But LiveCode 8 isn't doing me too many favours, so yeah, I'll give 7.1.1 another shot.

Thanks!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Server Questions

Post by FourthWorld » Wed Jan 06, 2016 4:58 pm

LiveCode 8 will be a wonderful release, probably the most significant change to the platform ever. But with that large scope comes more than a few regressions they're ironing out before it moves to Release Candidate, so I suspect you'll have a better time with v7.1.1 for now.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

A1Qicks
Posts: 79
Joined: Sat Dec 26, 2015 10:47 am

Re: Server Questions

Post by A1Qicks » Sat Jan 09, 2016 5:20 pm

So I've been looking at the sample chat server as suggested, and it's all largely making sense.

I did have to fix a line of code in the server, however, on closing the socket, there was a 'variable' missing on one line.

Something I need to understand is how to manage client and host when they're not on the same system. So say I have my server on my computer and my client on my phone. I know there's some port forwarding and stuff that needs to be done, but where can I look to start with that?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Server Questions

Post by FourthWorld » Sat Jan 09, 2016 7:53 pm

A1Qicks wrote:Something I need to understand is how to manage client and host when they're not on the same system. So say I have my server on my computer and my client on my phone. I know there's some port forwarding and stuff that needs to be done, but where can I look to start with that?
The challenge there is when the server is behind a firewall, where we'd want most wortkstations but will require port forwarding and possibly dynamic DNS when running a server.

Does your service provider give you a static IP address or dynamic?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

A1Qicks
Posts: 79
Joined: Sat Dec 26, 2015 10:47 am

Re: Server Questions

Post by A1Qicks » Sat Jan 09, 2016 9:32 pm

FourthWorld wrote:Does your service provider give you a static IP address or dynamic?
My provider offers dynamic - however, at the moment what I'm creating is more a proof of concept app to be developed into a full marketable option at a later point. When it's actually in use, I'll be paying for a server that has it all set up to ideal specifications instead (I hope).

(If I can ever get out of my day job).

A1Qicks
Posts: 79
Joined: Sat Dec 26, 2015 10:47 am

Re: Server Questions

Post by A1Qicks » Sun Jan 10, 2016 1:28 pm

I'm sure this is a stupid question, but I'm trying to create a system within the chat server for showing the list of users currently online.

It adds new users to the list without a problem, but I'm trying to work out how to remove a user from the list.

Currently I'm just looking at:

get lChatterArray[s] ##Gives the username
find it in field "HelpMe"
delete it

This is wrong, and I feel like I probably understand why it's wrong, but I don't know what's right, either.

Post Reply