Working offlineimprove this page | report issue
Working in offline mode
By using IBM MobileFirst Platform Foundation, it is possible to detect application connectivity failures and determine a course of action.
Application going offline and online can be detected in two ways:
- Explicitly, on invoking server-based procedures.
It is possible to define custom application behaviors for offline and online status.
The developer is responsible for maintaining the offline or online state within the application, and ensuring that the application can recover from failed attempts to connect to the server.
For example, before the application logs in a new user or accesses the server under a new user, the application must ensure that a successful logout was received by the server.
This tutorial covers the following topics:
By using methods
Connectivity loss can be detected in two locations in the application code:
- Application initialization – The
WL.Client.init()method, typically called from the
- Adapter procedure invocation – The
To add connectivity failure detection in either location, add the
onConnectionFailure property and specify a callback function to invoke if connectivity fails.
Implementation JS file
Offline and online events
Each time the MobileFirst framework attempts to access MobileFirst Server, it might detect that the application switched from offline to online status or conversely.
WL.Events.WORKLIGHT_IS_DISCONNECTEDevent is fired when connectivity to MobileFirst Server fails.
WL.Events.WORKLIGHT_IS_CONNECTEDevent is fired when connectivity to MobileFirst Server is restored.
The developer can also add event listeners to the above events and specify the callback functions to handle them.
WL.Events.WORKLIGHT_IS_CONNECTED are namespace constants, not strings.
More methods are provided by the MobileFirst framework to simplify online and offline development:
WL.Client.connect (options)– Attempts to establish a connection to MobileFirst Server and to return to online mode.
optionsis an object that contains the following keys:
onSuccess– Callback function to invoke when the server connection is established.
onFailure– Callback function to invoke when the server connection fails.
timeout– The number of milliseconds to wait for the server response before failing with request timeout.
WL.Device.getNetworkInfo()- This method is available for the Android and iOS environments.
A callback must be specified as a function parameter. The callback receives an object with the following properties:
carrierName– string. For example,
telephonyNetworkType– string. For example,
The heartbeat pings the server at specified intervals to verify connectivity.
You can use the heartbeat to periodically make sure that the application remains connected to the server.
WL.Events.WORKLIGHT_IS_DISCONNECTED events can be fired by the heartbeat in designated cases.
A developer can specify the heartbeat interval by using the
WL.Client.setHeartBeatInterval(intervalSeconds) API method.
The following sample shows an offline and online detection mechanism.
- An event listener for a
WL.Events.WORKLIGHT_IS_DISCONNECTEDevent is added to the document.
MyApp.connectionFailure()is invoked when the event fires.
WL.Client.connect()tries to establish a server connection.
- If the connection is successfully established,
- If the connection fails, a timeout is set for 5 seconds to invoke
Click to download the MobileFirst project.
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.