Connection Management¶
A client application running on a device or in a browser maintains a connection with the Backendless Real-Time Database. There is always only one connection for a single client, even though it may have multiple listeners for different data tables. The connection status can be tracked in the application using connection event handlers:
Connection Established Event¶
The event is dispatched when the client application establishes a connection with the Real-Time Database. Once a connection is established, real time database dispatches events triggered by the API calls to the client application. Add the following listener to be notified when the connection is established:
RTSubscription *subscription = [Backendless.shared.rt addConnectEventListenerWithResponseHandler:^{
NSLog(@"Client has been connected");
}];
let subscription = Backendless.shared.rt .addConnectEventListener(responseHandler: {
print("Client has been connected")
})
The "Connection Established" event takes place when the client application adds its very first API event listener. Additionally, the event may be triggered when the client reconnects with the real-time database after the connection is interrupted.
Disconnect Event¶
If the connection with the real-time database is dropped either due to network or server problems, the client application is notified via the following listener:
RTSubscription *subscription = [Backendless.shared.rt addDisonnectEventListenerWithResponseHandler:^(NSString *reason) {
NSLog(@"Client has been disconnected: %@", reason);
}];
let subscription = Backendless.shared.rt.addDisonnectEventListener(responseHandler: { reason in
print("Client has been disconnected: \(reason)")
})