Remote data persistence using cookies

Thinfinity VirtualUI: Remote data persistence using cookiesIn early May we started with a series of brief and simple, yet useful articles to help you take better advantage of the existing possibilities that Thinfinity® VirtualUI™ provides.

In the present note we will see a way to store remote data in the browser to be later retrieved from the application when needed.

Remembering login parameters, saving the last position on a map or a form’s data are typical cases of such a scenario. One possible way to address this need would to keep that information somewhere in the disk from where the application can recover it promptly. This would seem to be the most natural way of achieving that goal. But what would happen when we run the application remotely from the browser, greatly opening the range of users? In these cases, saving the information in the application does not seem to be the best way to do it; the most reasonable way, however, seems to be storing that information “near the user”; i.e, in the browser.

One of the most effective and classical methods of sharing information between the browser and any remote application is by using cookies. Cookies are small pieces of information that the application can store in each browser and later recover them in a clear way. In our particular case, we can create and enquire the value of a cookie directly from the application through the SetCookie and GetCookie methods of Thinfinity.BrowserInfo.



The SetCookie method has the following format:

VirtualUI.BrowserInfo.SetCookie(name, value, expirationDate);


Argument Data Type Description
name string Name of the cookie. With this name it is stored, properly identified, in the browser.
value string Value of the cookie
expirationDate string Stored in UTC/GMT format, indicating the expiration date of the cookie. If its value is “”, the cookie does not expire.

This example written in Delphi shows how store a data value (in this case the value of the username field) into a cookie:

procedure TForm1.Button1Click(Sender: TObject);
  if Trim(edtUsername.Text) = '' then begin
    ShowMessage('The username field must have a value');
  end else begin
    // saving the cookie when running with Thinfinity VirtualUI only.
    if VirtualUI.Active then begin
      VirtualUI.BrowserInfo.SetCookie('CookiesDemo_Username', edtUsername.Text, '');
      ShowMessage('Username was saved as a cookie in your browser.');

Please note the use of VirtualUI.Active before taking any action. This is because we only want to enable the use of cookies to those instances of the application running through VirtualUI. Also, the expirationDate argument is set to empty, indicating that the cookie will never expire.



The GetCookie method has a single argument:



Argument Data Type Description
name string Name of the cookie. With this name the cookie will be retrieved from the browser. If the cookie doesn’t exist, an empty string will be received.

And the example written in Delphi is:

procedure TForm1.FormShow(Sender: TObject);
  if VirtualUI.Active then
    edtUserName.Text := VirtualUI.BrowserInfo.GetCookie('CookiesDemo_Username');

Just like in the case of SetCookie, in the present case we also making sure that VirtualUI is active (i.e., that this instance of the application runs remotely, using VirtualUI).

Thus, with very few code lines, we can integrate a little further the browser with our application, and add minor enhancements that help improve the user experience.


Related Posts

Leave a comment

I accept the Privacy Policy * for Click to select the duration you give consent until.