RTC Forums
November 24, 2024, 02:40:40 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Sessions  (Read 5254 times)
DPerkins
RTC License
***
Posts: 37


« on: November 20, 2013, 05:43:03 PM »

I've read all the articles on sessions, yet I'm still unclear how they work.  Are sessions JUST a server side feature, or is the session object passed back and forth between the server and the client?  e.g  If I call a server Login function and include this line in the OnExecute func:

  ((TRtcDataServer*)Sender)->Session->asString["USER_ID"] = 27;

Should I then be able to access that on the client using

  String s = m_pRtcHttpClient->Session->asString["USER_ID"];

?

I have AutoSessions=true on both the server and the client.

Many thanks

Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: November 20, 2013, 05:58:46 PM »

Session is a Server-side storage for information specific to a Client. Session data is NOT automatically transmitted to Clients. If you want to send something to a Client, you need to do this explicitly, for example by using remote functions and sending the data back to the CLient by using the Result object.

Sessions are used to minimize the amount of repetitive traffic between a Client and a Server. Once a Session is created for a Client on the Server, any information relevant to the Server for that specific Client can be kept inside the Session, without the Client having to send that information in every request or remote call.

For example, user would log in to the Server. In the Login process, all the relevant user information about the Client would be sent to the Server (from the CLient) and stored inside a Session on the Server. The next time that Client contacts the Server (and sends the original Session ID), all that login information would be available to the Server through the Session, without the Client having to send it again. All the Server needs to access that information is the Session ID.

Without using Sessions, the Server only knows about the Client what the Client hast just sent in the current Request. With Sessions, there is persistent storage on the Server relevant for the Client.

By using Sessions, you are making your Server "Statefull", because the Server will be keeping Client-side state information. "Stateless" Servers, on the other hand, do NOT use Sessions. When you want to make a "Stateless" Server, the Client needs to send all the data required for processing a Request in every request, making requests "autonomous".

Best Regards,
Danijel Tkalcec
Logged
DPerkins
RTC License
***
Posts: 37


« Reply #2 on: November 21, 2013, 11:43:16 AM »

Thanks for that.  What effect does enabling sessions in the client have?  Just to return a session ID?

In my test application, I have a class which creates 10+ RtcHttpClients so that my application can fire off multiple requests simultaneously and not have them queued client side.  Is there anyway that these can all be associated with the same session, or must every RtcHttpClient instance also login and maintain their own session?
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 on: November 21, 2013, 01:02:19 PM »

TRtcHttpClient components keep their own Session objects internal. If you are using remote functions with automatic Sessions, then each TRtcHttpClient component will get a different Session ID from the Server, with a separate Session object on the Server side. There will be no mixing of Session data between Client components.

This is important, because requests from each Client can be executed at the same time on the Server, and Sessions can ONLY be used by one connection component at a time. Would the same Session ID be used by more than one physical connection, you would have to manually handle concurrent access to Session data on the Server side, which can get pretty complicated (see the other thread about Sessions and concurrent access for more info on that topic).

When working with RTC remote functions and automatic Sessions, instead of manually keeping track of Session status, use the OnLogin, OnLoginResult and OnLoginAborted events on the TRtcClientModule component to prepare all data required for a Session. The OnLogin event is called every time a new Session is being created on the Server for the Client and can be used to prepare all the data required by the Server for the Client.

PS. Only RTC remote functions (TRtcClientModule and TRtcServerModule components) provide automatic Session handling. If you are working with TRtcDataRequest and TRtcDataProvider components, you will have to manually create Sessions on the Server and handle sending of Session IDs back and forth.

Best Regards,
Danijel Tkalcec
Logged
DPerkins
RTC License
***
Posts: 37


« Reply #4 on: November 21, 2013, 03:07:25 PM »

If I use the the client OnLogin, OnLoginResult and OnLoginAborted, where/how are they used on the server? Nothing in Helpfile for OnLogin.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #5 on: November 21, 2013, 03:18:39 PM »

These are all Client-side events. The OnLogin event is used to prepare a remote function call, which will be sent to the Server just after a new Session is created, before any other remote function call is sent. In the OnLogin event, use the "Data" object the same way you would use the "Data" object of the TRtcClientModule component when preparing a remote function call.

For example, if you want to call a remote function "UserLogin" and pass on parameters "UserName" and "UserPwd" just after a new Session is created, you would write something like this in the OnLogin event:

with Data.newFunction('UserLogin') do
  begin
  asString['UserName']:='My username';
  asString['UserPwd']:='My password';
  end;

On the Server-side, the "UserLogin" remote function will be called, just like it would if you used the "Data" parameter on the TRtcClientModule component and posted a remote call using the "Call" method. The response you get from the Server will then be returned in the OnLoginResult event. And if there is a problem sending the remote call, the OnLoginAborted event will be called. These two events are the equivalents of OnReturn and RequestAborted events on the TRtcResult component.

Best Regards,
Danijel Tkalcec
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.025 seconds with 17 queries.