Using JSDOs to create mobile and web clients : Managing JSDO login sessions : Handling changes in session online status
  

Handling changes in session online status

Both progress.data.JSDOSession and progress.data.Session instances provide events, methods, and properties that you can use to identify and respond to changes in a client app's session online status. This online status reflects two basic network conditions, where:
*The connection between the device (or JSDO login session) in which the client app is running and an available Internet access point (such as a Wi-Fi router) has either been broken or re-established, also referred to as the device online status (or the JSDO login session online status).
*The availability of a web application to which the client app was previously connected has either been interrupted or re-established on a device (or JSDO login session) that is otherwise connected to the Internet, also referred to as web application online status or Data Object Service online status.
The following table briefly describes session instance events, methods, and properties that you can use to track a client app's online status.
Table 9. Events, methods, and properties for tracking app online status
Member
Brief description (See also the reference entry)
Returns a boolean that indicates the most recent online status of the current JSDOSession or Session object when it last determined if the web application it manages was available.
Fires when the current JSDOSession or Session object detects that the device on which it is running has gone offline, or that the web application to which it has been connected is no longer available.
Fires when the current JSDOSession or Session object detects that the device on which it is running has gone online after it was previously offline, or that the web application to which it is connected is now available after it was previously unavailable.
Determines the online state of the current JSDOSession object from its ability to access the web application that it manages, and for an OpenEdge web application, from detecting if its associated application server is running.
Determines the online state of the current Session object from its ability to access the web application that it manages, and for an OpenEdge web application, from detecting if its associated application server is running.
A number that specifies the duration, in milliseconds, between one automatic execution of the current JSDOSession or Session object's ping( ) method and the next.
Subscribes a given event callback function to an event of the current JSDOSession object.
Subscribes a given event callback function to an event of the current Session object.
Unsubscribes a given event callback function from an event of the current JSDOSession object.
Unsubscribes a given event callback function from an event of the current Session object.
Unsubscribes all event callback functions from a single named event of the current JSDO, JSDOSession or Session object, or unsubscribes all event callback functions from all events of the current JSDO, JSDOSession, or Session object.
You can identify a change in a client app's session online status by subscribing handlers to the online and offline events of a JSDOSession or Session instance using the object's subscribe( ) method any time after you create the object. When the JSDO login session detects that its previously online device has gone offline or its previously connected web application is no longer available, the session fires its offline event. When the session detects that its previously offline device has come back online or its previously unavailable web application connection has, again, become available, the session fires its online event.
When a subscribed event fires, the handler signature includes:
*A reference to the session instance on which the event was fired
*A reference to a request object (if any) that was sent for a Data Object Service request that triggered the event
*For the offline event, a reason that an offline status was detected, such as its device going offline.
Note: If the offline or online event fires because of a change in device online status, no request object reference is returned and the handler's request object parameter is set to null.
A JSDO login session supports several ways to identify either the device online status or the web application online status for Data Object Services managed by a session instance:
*Subscribing handlers to the online and offline events of the session instance using the instance's subscribe( ) method, as described above. Again, these events fire whenever there is a change in session online status for any reason.
*Invoking the ping( ) method on the session instance. This method returns the current online status of the session instance. It sends a request to the web application that is managed by the session instance and attempts to determine if the Data Object Services provided by the web application are available.
On a JSDOSession instance, this method immediately returns a Promise object on which you can register various callback functions using Promise object methods. These callbacks execute depending on the conditions of method completion. For example, if the callback executes that you register using the done( ) method, the JSDOSession instance is online with access to its Data Object Services. If the callback executes that you register using the fail( ) method, the JSDOSession instance is offline either because the device is offline or there is a problem with the web application or Data Object Service connections.
On a Session instance, this method can execute either asynchronously, with results returned in a callback function that you specify, or synchronously with a boolean value to indicate the online status and properties of an object parameter that are set to provide more information.
In addition, if the current online status identified by ping( ) has changed from the most recent status detection (either from a call to ping( ) or a JSDO request for a Data Object Service), the session instance also fires its corresponding offline or online event, to which any subscribed event handler can respond. Note that ping( ) returns online status results only between a successful execution of the login( ) method and successful execution of the logout( ) method on the same session instance.
*Setting the pingInterval property on the session instance to a value in milliseconds greater than zero (0). This setting causes the instance to automatically and periodically invoke its ping( ) method once its login( ) method has successfully executed. The minimum time between the completion of one execution of ping( ) and the start of the next execution of ping( ) is the number of milliseconds you assign. Each automatic execution of ping( ) only fires an offline or online event on its session instance if the method detects a change in session online status. Automatic ping( ) execution returns no other results.
*Testing the value of the connected property on the session instance. This boolean value indicates the most recent online status of the current session instance when it last determined if the web application it manages was available. If the property value is true, the instance most recently determined that the session is connected and logged in to its web application. If its value is false, the session was last found to be disconnected.
This property is first set to true from a successful invocation of the login( ) method on the session instance. However, to obtain the most current online status, if different, you must either invoke the instance's ping( ) method or make a JSDO request to one of the instance's Data Object Services, which updates the value of its connected property if its online status has changed. This property is also set to false after you invoke the logout( ) method on its session instance.
Note that from the point of successful invocation of login( ) on a session instance, a JSDO login session is always initially online. So, the first change in device or Data Object Service online status is indicated by a firing of the offline event by the session instance in response to detecting that the device or its most recently connected web application has gone offline.
Note also that all attempts to detect the current availability of Data Object Services provided by a connected web application, such as executing ping( ) or making JSDO requests, return results from the hosting web server only after successfully starting and maintaining a user login session on the web application from a device that is online (that is, by invoking login( ) on a session instance for the web application and not yet invoking logout( )). However, you can always receive results from changes in the device's online status by subscribing to offline and online events from the first point that you create a session instance (and regardless if you have yet started a JSDO login session) until you either destroy the session instance or unsubscribe to these events using the unsubscribe( ) or unsubscribeAll( ) methods.