Nirvana Flex : Creating a Session

To interact with a Nirvana Server, the first thing to do is create a Nirvana Session object, which is effectively your logical and physical connection to a Nirvana Realm.

Creating a Nirvana Session Object

The Flex nSessionFactory create function takes four parameters:

  • attributes : the nSessionAttributes for the Nirvana Realm Server. To create a nSessionAttributes it requires a RNAME and a initialConnectionRetry count
  • username : The current user's username. This allows you to link sessions to already authenticated users, irrespective of what authentication technology you are using.
  • appName : An arbitrary string which should ideally be unique to this application. Again, this is to help you monitor sessions using the Enterprise Manager GUI.
  • errorCB : The callback if an error occurs.

The Flex code snippet below demonstrates the creation of an nSession and a nSessionAttributes object:

var RNAME      : String = "nhp://nirvanahost:80";
var username   : String = "myUserName";
var appName    : String = "MyApplicationName";
var attributes : nSessionAttributes = new nSessionAttributes(RNAME,5);
var mySession  : nSession = nSessionFactory.create(attributes, username, appName, errorCB);

Initializing a Nirvana Session

Once the nSession object has been created, it must be initialized to create a connection to the server:

mySession.init(sessionInitCB);

Note that the nSession.init() call is asynchronous; it returns immediately, allowing Flex clients to continue processing due to their single thread.

To know when an nSession.init() call has completed, and also when server disconnects and reconnects, the init() method takes a callback function.

To enable the use of DataGroups and to create an nDataStream, you should pass an instance of nDataStreamListener to the init call.

public class SimpleStreamListener implements nDataStreamListener{
    //implement onMessage callback for nDataStreamListener callbacks
}

var myListener:nDataStreamListener = new SimpleStreamListener();
mySession.init(sessionInitCB, myListener);

After initialising your Nirvana session, you will be connected to the Nirvana Realm. From that point, all functionality is subject to a Realm ACL check. If you call a method that requires a permission your credential does not have, you will receive an nSecurityException. Click here for more information on Nirvana ACLs.

Creating a Multiplex Session

It is also possible to create a Multiplex Session. This involves creating a session from an existing session, either via the appropriate nSession object or its associated nSessionAttributes. These two sessions will now appear to act as normal sessions, but will, in fact, share a single connection. Below is an example of how to create multiplex sessions, both via nSession and via nSessionAttributes:

// original session
private var _session:nSession;

// original session attributes
private var attributes:nSessionAttributes;

// construct a new session with the nSession object
nSessionFactory.createMultiplexed(_session, nSessionCB, errorCB);

// construct a new session with the nSessionAttributes object
nSessionFactory.createMultiplexedWithAttributes(attributes, nSessionAttributesCB,
"flex", "session3", errorCB);

//...

private function nSessionCB(e:*):void {
    _session2 = e;
    _session2.init(sessionInitCB, this, true);
}

private function nSessionAttributesCB(e:*):void {
    _session3 = e;
    _session3.init(sessionInitCB, this, true);
}

For an example of multiplexed sessions in Flex, please see the Flex Multiplex Tradespace sample application in the Nirvana download.