There exist cases where you could need to disconnect a user from Lightstreamer Server. For example, if a user logs in multiple times from different clients and you don't want to support multiple push sessions for the same account. Or perhaps you may want to disconnect a user after a given amount of time since login.
Let's see some different scenarios and solutions.
A) You have one instance of Lightstreamer Server only and you don't want to allow more than one session per user.
In this case, you will simply throw a
ConflictingSessionExceptionfrom the
notifyNewSession method of your Metadata Adapter.
B) You want to force a session termination (and disconnection of that user) at any time, on any instance of Lightstreamer Server, from any piece of code running anywhere.
You have three ways of doing that, depending on the Lightstreamer edition you are using and the technology yo employ, as detailed below:
B1)[Lightstreamer Vivace - Java only] Use JMX (Java Management eXtensions, which is a standard specification that allows an application to be controlled both internally and externally and both manually and automatically). Lightstreamer Server exposes a set of MBeans that give full control over the engine and the sessions.
Since Server version 6.0, the simplest way to terminate a session through JMX is by calling the
destroySession(sessionId) method on the ResourceMBean object.
With previous versions, you just need to call the
destroySession() method on the appropriate SessionMBean object.
You can access MBean objects both from inside your Adapter and from any external Java application.
A
source code example is available from a
previous post , where the Metadata Adapter causes a session to be closed after it has lived longer than five minutes. The example is based on the SessionMBean technique.
B2)[Lightstreamer Moderato,Allegro,Presto,Vivace - any technology] Send a special HTTP request to a Lightstreamer Server instance to force a session termination. The sessionId of the session to terminate is sent as a parameter. SessionIds are random and complex enough to be quite impossible to guess, so no specific authentication is required to send such request.
For Lightstreamer Presto and Vivace, the syntax of this HTTP request is explained in section 4.3.4 of “Network Protocol Tutorial.pdf”. A
source code example is available from a
previous post (see first code snippet), where the Metadata Adapter causes a session to be closed after it has lived longer than five minutes.
For Lightstreamer Moderato and Allegro, the syntax of this HTTP request is a bit different and is directly explained in the
source code example of the
previous post (see second code snippet), where the Metadata Adapter causes a session to be closed after it has lived longer than five minutes.
This solution works with Lightstreamer Presto and Vivace too, making it a truly general solution that works with all the editions of Lightstreamer.
Now that you have general means to force a session termination both locally or remotely, you can leverage such means to build a simple system for managing session limits across the server cluster. Basically, your Metadata Adapter instances can keep track, through a central DB or some memory sharing mechanism, of the node on which each session for each user is living. When a user tries to create a new session, you query the DB to know if they are allowed to have one more session. If they aren't, you could decide to force the termination of an existing session.