ankor-io-deprecated/ankor-framework

Persistent web session???

AndyObtiva opened this issue · 3 comments

I am very impressed with Ankor's simplicity and clean architecture, and look forward to more features in the future. I ran across an issue while trying to create an app that allows users to maintain a session per client login for multiple days without doing my own coding with cookies (given Ankor tries to abstract as much web details away as possible)

Is there a built-in way to maintain a web session explicitly that persists for a certain time (say 30 days just like cookie or http session options allow) so that every time the user opens the browser, they reuse the same Ankor session? I realize I could probably could my own implementation by use of cookies, but was hoping Ankor exposes its session information without killing it when closing the browser window (which I experienced) to allow a persistent web session across multiple connections to the application.

Andy,
Short answer: yes, built-in support (= concept) on server side, no built-in support on the client side yet.
Long answer: There are some terms in Ankor that I must explain first...
We distinguish:

  • Model
  • Model Instance
  • Model Session
  • Model Connection

Model = the logical structure of model data, the Java classes
Model Instance = the actual POJO instances during runtime, which belong to one user (or multiple collaborating users), Note: managing model instances is up to the application
Model Session = Ankor's management envelope for the Model Instance(s) belonging to one user. Ankor assigns an unique id and manages event listeners, etc. - see https://github.com/ankor-io/ankor-framework/blob/stable/ankor-core/src/main/java/at/irian/ankor/session/ModelSession.java
Model Connection = the "physical" connection of one client app instance to a model session

There is no equivalent to a web sessions with timeouts in Ankor. Reason for this is: Ankor automatically manages Model Sessions based on the actual connections. As soon as a client connects to a model instance the first time, a new model session is automatically created. Multiple connections per session are possible: multiple browser windows of the same user, or a connected browser client and desktop client at the same time, multiple collaborating users, etc.). As soon as the last connection to a session is closed (explicitly by the client or because of a connection timeout), the model session is automatically cleaned up. Keep in mind that this does not close or remove the model Instance! It's totally up to the application (developer) to manage the lifecycle (creation, cleanup, timeouts, persistence, etc.) of the model data in memory.
So, there is no simple answer to your question, I fear ;-) However, there is a "built-in" way in Ankor for the use case you have in mind - at least on the server side as I mentioned: on every connection the client app may provide custom connection params. The browser client app could for instance provide cookie data during the connect, which are then piped through Ankor and handed over to the Application - see "lookupModel " and "createModel" in https://github.com/ankor-io/ankor-framework/blob/stable/ankor-core/src/main/java/at/irian/ankor/application/Application.java
Your application then can manage those model instances based on (cookie) identified users.

Hope that helps,
Manfred

Thank you Manfred. I think I have a plan to implement persistent session support for my application. I'll report back once done to let you know how things went.

By the way, I implemented an initial web UI login page for a desktop JavaFX 2.2 application by reusing its View Models with Ankor and data-binding via knockout.js. I will blog about it by the end of next month at http://andymaleh.blogspot.com

Thanks Manfred, I got it to work with this Java code:


public class WebApplication extends GenericApplication {
   public WebApplication() {
      setName("Web Stage");
      setDefaultModelType(ModelRoot.class);
   }
   
   @Override
   public void shutdown() {
      /* No cleanup necessary since we are maintaining models for long sessions */
   }
   @Override
   public void releaseModel(String modelName, Object modelRoot) {
      ModelRoot root = (ModelRoot)modelRoot;
      if (!root.getLoginViewModel().getRememberMe()) {
         super.releaseModel(modelName, modelRoot);
      }
   }
   
}

And this JavaScript client code initialization snippet:


...
  var baseUtils = new BaseUtils();
  var sessionId = cookies.getCookie("sessionId") || baseUtils.uuid();
  var cookieExpirationInDaysFromToday = 90;
  cookies.setCookie("sessionId", sessionId, cookieExpirationInDaysFromToday);
  var transportParams = {
      "connectProperty": "root",
      "connectParams": {
        "at.irian.ankor.MODEL_INSTANCE_ID": sessionId
      }    
  }
  window.ankorTransport = new WebSocketTransport("/websocket/ankor", transportParams);
  window.ankorSystem = new AnkorSystem({
      debug: true,
      modelId: "root",
      transport: ankorTransport,
      utils: baseUtils
    });
...

Thank you for all your help.

Andy