Scalable sessions for advanced web applications

Automatically translated from English

Nowadays, web applications are essential modern tools. As machines and processors become more and more powerful, your web applications must constantly meet performance requirements. This is why with 4D v18 R6, the 4D Web Server offers a new kind of web session: the scalable Web session.

Let’s find out more!

HDI: Scalable web sessions

Previously, you were used to the Automatic session management option on the Web page of the Structure Settings. It was convenient to keep session data alive on the server.

While 4D’s Web Server is preemptive and can take advantage of all available cores to answer web processes, each session (representing the browser from an end user) was handled through a single process, so several requests coming at once from this browser were handled sequentially, one after another

Now, scalable web sessions are able to handle several processes in preemptive mode. This means that scalable web sessions can handle several user agent requests at the same time. This greatly improves performance.

And that’s not all! These new scalable web sessions can share data between these processes.

work with scalable web sessions

Want to work with new scalable web sessions? Nothing could be simpler. Simply activate the new web settings and start your web server.

Scalable web sessions are handled through an object

Once you’ve activated the setting, you can handle the session through an object. The new Session command returns this object.

 

It’s available in any web process triggered by 4D tags, 4D actions, REST functions (ORDA Data Model Classes), and databases methods such as On Web Authentication / On Web Connection.

Thanks to this object, you can handle many aspects of your web session.

Here’s an example of what you’ll see in the debugger:

share data between processes

This Session object comes with a storage property which is a shared object. You can store any data you want to share between requests coming from the user agent on the session in the storage property.

example

In this example, we have a CRM application. Each salesperson manages their own client portfolio. The database contains two linked dataclasses: Customers and SalesPersons (a salesperson has several customers).

 

 

Let’s say your web server is started on your machine (IP) using an HTTPS port (port).

When the URL IP:port/4DACTION/authenticate?userId=1 is entered in a web browser by a salesperson, the authenticate method is called.

The top three customers of the salesperson are stored in the session. Here is the code of the authenticate method:


var
$indexUserId : Integer
var $userId : Integer
var $info : Object
var $userTop3 : cs.CustomersSelection
var $sales : cs.SalesPersonsEntity
ARRAY TEXT($anames; 0)
ARRAY TEXT($avalues; 0)

WEB GET VARIABLES
($anames; $avalues)
$indexUserId:=Find in array($anames; "userId")
$userId:=Num($avalues{$indexUserId})
$sales:=ds.SalesPersons.query("userId = :1"; $userId).first()
If ($sales#Null)
  Use (Session.storage)
    If (Session.storage.myTop3=Null)
     $userTop3:=$sales.customers.orderBy("totalPurchase desc").slice(0; 3)
     Session.storage.myTop3:=$userTop3
    End if
   End use

end if
WEB SEND HTTP REDIRECT("/sessionStorage.shtml")

Next, this top three are accessible by any request coming from the user agent (e.g., in an SHTML page):

<center><h3>Your top 3 customers</h3></center>
<table class="table">
  <!--#4DCODE
  $i:=0
  $myTop3:=Session.storage.myTop3
  -->
  <tr><th>Name</th><th>Total purchase</th></tr>
  <!--#4DLOOP ($i<$myTop3.length)-->
   <tr>
    <td ><!--#4DTEXT $myTop3[$i].name--></td>
    <td ><center><!--#4DTEXT $myTop3[$i].totalPurchase--></center></td>
   </tr>
   <!--#4DEVAL $i:=$i+1-->
  <!--#4DENDLOOP-->
</table>

And here is the result:

upcoming actions with the session

We’re working on an accurate privileges management for the future. So that you will be able to grant access + operations on data according to privileges.

For now, you can put user’s information in the session thanks to the setPrivileges() function. This function will be enhanced to handle more actions for privileges in a future release. 

example

var $indexUserId : Integer
var $userId : Integer
var $sales : cs.SalesPersonsEntity
var $info : Object
ARRAY TEXT($anames; 0)
ARRAY TEXT($avalues; 0)
WEB GET VARIABLES($anames; $avalues)
$indexUserId:=Find in array($anames; "userId")
$userId:=Num($avalues{$indexUserId})
$sales:=ds.SalesPersons.query("userId = :1"; $userId).first()
$info:=New object()
$info.userName:=$sales.firstname+" "+$sales.lastname
Session.setPrivileges($info)

So you can customize your web pages by adding something like this:

<center>
<h3>
Hello <!--#4DEVAL Choose(Session.userName # ""; Session.userName; "nobody")-->
</h3>
</center> 

Here is the result:

As you may have noticed in the debugger screenshot, scalable web sessions close (timeout) after 60 minutes of inactivity (i.e., no requests solicits the session).

Download the HDI above to discover more and join the discussion on the 4D forum!

 

 

Avatar

• Product Owner •

Marie-Sophie Landrieu-Yvert has joined the 4D Product team as a Product Owner in 2017. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.

Marie-Sophie graduated from the ESIGELEC Engineering School and began her career as an engineer at IBM in 1995. She participated on various projects (maintenance or build projects) and worked as a Cobol developer. Then she worked as an UML designer and Java developer. Lately her main roles were analyzing and writing functional requirements, coordinate business and development teams.