I’m looking for a way to mange network connection drop if phone gets disconnected from Backendless server.
I tried to add a disconnecteventlistener, but it doesn’t get called when I try to manually turn off wifi on my phone.
let _ = Backendless.shared.rt.addDisсonnectEventListener(responseHandler: { reason in
print("Client has been disconnected: \(reason)")
})
Is there a way to get notified when global connection to Backendless drops?
I’m trying to implement a function that attempts to rejoin the chat channels, and reload data if network connection ever drops when app is in foreground.
Another question, I currently have a function that rejoin and reload when app enters foreground from background too. Does Backendless SDK support receiving messages when app is in background( in app switcher), to save the reload api call if user switches app frequently?
I noticed another strange behavior. When I first start the app, for each message from a chat channel, the response handler block below gets called once, which is expected. However when app goes to background, channel connection drops, and after I rejoin the channel when app reenters foreground, the response handler gets called twice when a message is published in the channel(result in duplicated messages on the ui).
let station = Backendless.shared.messaging.subscribe(channelName: channel)
station.subscribeToStation
private func subscribeToStation(station: Channel){
let _ = station.addMessageListener(responseHandler: { message in
self.createReceivedMessage(message: message)
}, errorHandler: { fault in
print("Error: \(fault.message ?? "")")
})
}
This bit below check if the channel connection dropped(and rejoin if did) when app reenters foreground
let station = Backendless.shared.messaging.subscribe(channelName: channel)
if !station.isJoined{
self.subscribeToStation(station: station)
}
Is Backendless SDK trying to reconnect previous channel internally? result in duplication of subscription?
Is there a way to get notified reliably when channel reconnection is attempted(so I can set a timer to verify channel connection with isjoined)
Or if there is a way to get notified when channel join is successful(or unsuccessful)?
I’m now using a 2 sec delay timer to check if connection to channel has been established, no duplication observed yet from this part. This block is after the initial subscription attempt when app start up.
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
if !station.isJoined{
self.subscribeToStation(station: station)
}}
Is there a way to get notified when global connection to Backendless drops?
You can use the connection management events to get notifications when your device was disconnected from Backendless.
Does Backendless SDK support receiving messages when app is in background?
Backendless uses Socket.IO for real-time functionality and according to this answer from their developer and Apple restrictions - socket connection drops when app enters background.
Maybe this answer or this one can be useful.
The response handler gets called twice when a message is published in the channel.
The response handler can be called twice if you subscribed twice.
If you don’t subscribe twice - it’s weird behaviour. I’ve just checked and got the expected one message. Please provide the simple code sample which reproduces this issue.
Also there was a bug when subcriptions were not resubscribed after reconnection. This bug is fixed and a new version 5.7.0 of Swift-SDK will be available later today.
If there is a way to get notified when channel join is successful(or unsuccessful)?
You can use this method:
func addConnectListener(responseHandler: (() -> Void)!, errorHandler: ((SwiftSDK.Fault) -> Void)!) -> SwiftSDK.RTSubscription?
let _ = channel.addConnectListener(responseHandler: {
print("Connected to channel")
}, errorHandler: { fault in
print("Failed to connect to channel")
})
I think the duplication is caused by this block after app reenters foreground.
Seem like both my code and Backendless sdk were trying to reconnect, end up with duplicated subscription.
if !station.isJoined{
self.subscribeToStation(station: station)
}
Should I expect Backendless sdk to manage app connections going back to the background and back by itself?
Should I just delete custom reconnect code and let Backendless handle reconnection automatically?
I updated the SDK
When I tried to manually disable wifi, the block below still doesn’t get called.
Under which condition would the disconnect event get called?
let _ = Backendless.shared.rt.addDisсonnectEventListener(responseHandler: { reason in
print("Client has been disconnected: \(reason)")
})
You can use next subscriptions to handle reconnection and resubscription:
let _ = Backendless.shared.rt.addConnectEventListener(responseHandler: {
print("RT connected")
})
let _ = Backendless.shared.rt.addDisсonnectEventListener(responseHandler: { reason in
print("RT disconnected: \(reason)")
})
let _ = Backendless.shared.rt.addConnectErrorEventListener(responseHandler: { reason in
print("RT error: \(reason)")
})
let _ = Backendless.shared.rt.addReconnectAttemptEventListener(responseHandler: { reconnect in
print("RT trying to reconnect")
})
let channel = Backendless.shared.messaging.subscribe()
let _ = channel.addStringMessageListener(responseHandler: { message in
print("\(message)")
}, errorHandler: { fault in
print("Error: \(fault.message ?? "")")
})
let _ = channel.addConnectListener(responseHandler: {
print("Channel connected")
}, errorHandler: { fault in
print("Error: \(fault.message ?? "")")
})
I’ve found an issue when disconnect listener doesn’t trigger without ConnectErrorEventListener, so please add it. This issue will be fixed as soon as possible.