Tutorial: Get connection state

Tips and Tricks Get connection state

Webcom connection state

The ServerlessDb Service implemented by the Webcom SDK manages a cache of sent and received data in order to manage disconnections. It is possible to force connection/disconnection as well as to know the current connection status.

In JavaScript, use the connect() and disconnect() methods on the ServerlessDb object to force connection or disconnection. You can also read the connection status from the currentState property or subscribe to the ".info/connection/connected" virtual node to be notified of its changes in real time (replace “<your-app>” with your actual application identifier):

// const app = Webcom.App("<your-app>"); // UNCOMMENT if you haven't yet an instance of your app!
const database = app.serverlessDb;

database.rootNode.relativeNode(".info/connection/connected")
    .subscribe(
        Webcom.Event.ValueChange,
        Webcom.Callback(snapshot => {
            const connected = snapshot.val(); // equivalent to: connected = database.currentState.connection.connected
            if (connected) {
                // Connected to Webcom server (page loaded, network up...)
            } else {
                // Disconnected from Webcom server (network lost...)
            }
        })
    );
// ...
database.disconnect(); // disconnect the websocket of the Datasync service
// ...
database.connect(); // reconnect the websocket of the Datasync service

In Kotlin, set the isConnectionEnabled property of the DatasyncService to true or false in order to force connection or disconnection. The actual connection state is given by the isConnected() method of the DatasyncState, which may be either subscribed to using the subscribeToStateChange() method or directly retrieved from the state property:

import com.orange.webcom.sdk.datasync.subscription.DatasyncState.ConnectionState.*

val app = WebcomApplication.default
val datasync = app.datasyncService
val manager = datasync.createManager()
manager.subscribeToStateChange { // it: DatasyncState
  val connected = it.isConnected() // equivalent to: val connected = datasync.state.isConnected()  
  println("the Datasync service is now ${if (connected) "connected" else "disconnected"}")
}
// ...
datasync.isConnectionEnabled = false // disconnect the websocket of the Datasync service
// ...
datasync.isConnectionEnabled = true // reconnect the websocket of the Datasync service
let datasyncManager = Webcom.defaultApplication.datasyncService.createManager()
datasyncManager.subscribeToStateChange { state in
    if state.isConnected {
        print("Connected to Webcom server")
    } else {    
        print("Disconnected from Webcom server")
    }
}

Warning: on application loading, connection may take some time to establish and thus the first call to the callback may indicate that connection is down. A short time later, as soon as the connection establishment is successfully completed, the callback is called again indicating the connection is up.