Server Sent Events - Prevent Closing of Socket

Are you using LiveCode to create server scripts or CGIs?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 140
Joined: Mon Mar 12, 2007 12:06 pm

Server Sent Events - Prevent Closing of Socket

Post by n.allan » Mon Jun 29, 2020 4:52 pm

I am messing about with server sent events in livecode server. I have made a start by translating the php example found here ... ce/basics/

Here is my very basic code to start with...

Code: Select all

put header "Content-Type: text/event-stream"
put header "Cache-Control: no-cache"
put header "Connection: keep-alive"
put "data: Server Time:" && the time && "Server Date:" && the date & crlf & crlf
It resides in a file called "" on the root of my server

I created an html called ssetest.html with the script as per example (I have replaced "events.php" with "")

In the browser console the page keeps transmitting the string "Server Time XXXXX Server Date: XXXXX" every 3 seconds.

I believe lc server is closing the connection every time i "put" data even though I have specified "Connection: keep-alive" in the header response.

Is there any way way to keep the connection alive but still transmit data back to the client?

I've had livecode hosting for years and it's about time I started to have a play with it.

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

Re: Server Sent Events - Prevent Closing of Socket

Post by FourthWorld » Mon Jun 29, 2020 5:49 pm

See note on the limitations of keep-alive in HTTP 1.1 and later: ... n#HTTP_1.1
n HTTP 1.1, all connections are considered persistent unless declared otherwise.[2] The HTTP persistent connections do not use separate keepalive messages, they just allow multiple requests to use a single connection. However, the default connection timeout of Apache httpd 1.3 and 2.0 is as little as 15 seconds and just 5 seconds for Apache httpd 2.2 and above. The advantage of a short timeout is the ability to deliver multiple components of a web page quickly while not consuming resources to run multiple server processes or threads for too long.
Keep-alive is useful for making multiple requests to the same server to render a page, but does not appear to be designed for ongoing persistence.

In the absence of web sockets, most use polling.
Richard Gaskin
LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 140
Joined: Mon Mar 12, 2007 12:06 pm

Re: Server Sent Events - Prevent Closing of Socket

Post by n.allan » Mon Jun 29, 2020 7:44 pm

Thanks for the reply. I am completely new to server technology. I will keep digging as it may be an Apache issue.

I have implemented server sent events in my own hand rolled LiveCode server (which admittedly will not be 100% compliant) and this works for me. The web browser shows the connection staying open, I can transmit Server Sent Events at my discretion and they are correctly recognised as Events by the browser in the debugger.

Code is simplified below...

Code: Select all

global gClients

accept connections on port 80 with message ... //blah blah .... which triggers "newRequest" when web page is requested

on newRequest pClient, pRequest
 //.... process headers here, lots of httpd stuff, but the Server Sent Event Section is as follows...
 	if line 1 of pRequest contains "/"  then -- check if the request is for Server Sent Events
 		// keep track of the clients
      		if pClient is not among the items of gClients then
        		 put pClient & comma after gClients
     	 	end if
      sseResponse pClient
      return empty -- do not continue with complete web request
   end if
   //... lots of other httpd type stuff
end newRequest

   on sseResponse pClient, pData, pEvent
   if pData is empty then put the seconds into pData -- should never be empty
   local tResponseHeader, tResponseBody
   put statusLine(200) into tResponseHeader -- function simply returns "HTTP/1.1 200 OK" & crlf or some such
   put "Cache-Control: no-cache" & crlf & \
         "Content-Type: text/event-stream" & crlf & \
         "Connection: keep-alive" & crlf after tResponseHeader
   put "id:" && the seconds  & "/n" & crlf into tResponseBody
   if pEvent is not empty then
      put "event:" && pEvent & "/n" & crlf after tResponseBody
   end if
   put "data:" && pData & "/n/n" & crlf after tResponseBody
   write tResponseHeader & crlf & tResponseBody & crlf to socket pClient
end sseResponse
This is the only way I have been able to deal with things like multipart-requests and Server Sent Events up until now. I have complete control of the TCP connection. I was hoping to migrate some of the features I had implemented to the "cloud".

As an aside. I did skim across the spec for HTTP/1.1 and I too interpret this as "connections are kept open unless "Connection: close" is specified. Only then will I terminate the connection and tidy up my client lists.

Long Story Short... I wish we could control the TCP connection for web requests using lc. I don't see why the connection is automatically closed after writing/putting data. (Especially if I have specified "Keep Alive" in the header.

Post Reply

Return to “CGIs and the Server”