In 4D applications, four types of sessions exist 4D Remote session, Stored Procedure session, Mobile session, and Web session.
The Session command already returns Web or Mobile sessions. This gives you access to a wealth of information about the session and an object shared by all processes in the session.
In 4D 20 R5, the Session command has been extended to the Client/Server connection and stored procedure.
4D session
Different type
The definitions of a stored procedure session or a remote session are:
Stored Procedures session
When you call a method with the Execute on server command, 4D creates a “Stored procedure” process. All “Stored procedure” processes belong to the same session, which is dedicated to stored procedures.
Remote session
Methods with the “Execute on server” property are executed in the twin client process on the server. All client twin processes are executed in the same session.
Session class
For a remote session or a stored procedure session, the Session command returns a 4D.Session object containing the following properties:
- id – Unique identifier of the session
- info – All information about the session
- storage – A shared object that can be used to store information available to all processes of the remote session
- userName – The user name associated with the session
The same functions of the Web session are available for writing generic code. But do nothing and return always the same value. We invite you to see the documentation of the Session class.
Example:
{ "id": "B022A91187EF46C6B4D58C2A9BDE3C91", "info":{ "type": "remote", "userName": "Designer", "machineName": "John Computer", "systemUserName": "John Doe", "IPAddress": "localhost", "hostType": "mac", "creationDateTime": "2024-03-27T10:11:31Z", "state": "active", "ID": "B022A91187EF46C6B4D58C2A9BDE3C91", "persistentID": "24336199BADF4BFE945B109E565D1189" }, "storage": { "settings": { "text": "test" } }, "userName": "Designer" }
Share data between processes
When a new session is created, the “Session.storage” property shall be instantiated with an empty shared object. Then, you can store any data you want to share between processes in the same session.
Use (Session.storage)
Session.storage.settings:=New shared object("property"; $value; "property2"; $value2)
End use
Use cases
User authentication and verification process
You launch a user authentication and verification procedure when a client connects to the server. For example, this may involve entering a code sent by e-mail or SMS into the application. You then add the user information to the session storage, enabling the server to identify the user. This way, the 4D server can access user information for all client processes, enabling customized code to be written according to the user’s role (e.g., manager, HR, etc.).
Restrict data
With this new session type, you can restrict the data displayed to the connected user. For example, in a customer management application, the sales representative only has access to his customer data. All the information on this new feature is explained in this blog.
Next
Feel free to join us on the 4D Forum if you have any questions or want to share your experience or use cases.