Table of Contents
Just a second...

Reconnecting with the JavaScript Classic API

The JavaScript® Classic API supports reconnection. If you have reconnection enabled and you lose your connection to a server, you can reestablish it, using the same client ID and with the client subscriptions preserved.

The JavaScript API listens for pings from the server and raises an event if the connection to the server is lost.

To enable the liveness monitor, set the enableLivenessMonitor parameter to true inside the client connections details. For example:
var connectionDetails = { ...
    enableLivenessMonitor : true, 
... };

DiffusionClient.connect(connectionDetails);

The Diffusion™ server sends out pings at regular intervals. The length of this interval is configured at the server by using the system-ping-frequency element in the Connectors.xml configuration file.

The liveness monitor in the ActionScript® client library listens for the pings from the server and uses them to estimate the ping interval. The liveness monitor takes an average of the time between the pings it receives to estimate the ping interval. It revises this estimation each time it receives a ping, until it has received ten pings. After ten pings the liveness monitor has obtained the estimated ping interval that it uses for the rest of the client session.

If the liveness monitor does not receive a ping within a time interval equal to twice the length of the estimated ping interval, it considers the connection lost and raises a connection lost event.

Connection lost events can be raised by the liveness monitor or triggered by other events, such as an unexpectedly closed connection.

You can implement an event listener in your client that listens for a connection lost event and reacts to it by using the reconnect() method to reestablish the connection.

Reconnection example

To reconnect after you lose connection, you must use the reconnect() method. You cannot reconnect an aborted client.

The following code shows how to setup an event listener for connection events, if the connection has been lost how to reconnect and how to tell if you have successfully reconnected the client.

var connectionDetails = {
        onCallbackFunction : function( isConnected, isReconnect) {
                if( !isConnected && isReconnect ) {
                        DiffusionClient.reconnect();
                }      
        },
        onLostConnectionFunction : function() {
                DiffusionClient.reconnect();
        }          
};
 
DiffusionClient.connect(connectionDetails);

For more information, see JavaScript Classic API documentation.