SMACK connectionClosedOnError occurs when WiFi is changed. How to handle reconnection - android

I and working on a Chat Application using SMACK 4.1.7.
I receive connectionClosedOnError when Wifi is changed from A to B and I get Network Change Event through broadcast receiver.
I need to figure out how to handle this to keep connection active with server.

You should try using the smack ReconnectionManager for handling automatic reconnect after the network switches.
http://download.igniterealtime.org/smack/docs/4.1.7/javadoc/
Smack's ReconnectionManager takes care of handling the reconnect anytime your connection is dropped unexpectedly.
You might also want to check this post
https://ramzandroidarchive.wordpress.com/2016/03/14/handling-connection-break-issue-in-smack-4-1/
It gives a good explanation on how to maintain a persistent connection.

Related

Android XMPP - not receiving messages after an hour of idle mode

I have a service that runs every min to check if the global XMPPConnection object is connected and authenticated. What I found is that after an hour or so of idle mode, the connection object shows it is connected and authenticated but it does not receive any messages.
The connection seems to be broken without the Connection object knowing about it. Please let me know what I could implement to check if the XMPPConnection object is truly connected and can receive messages even after a long while of inactivity.
Appreciate your help.
hi If you are using openfire then go to Server settings then Client connection and change settings to Do not disconnect clients that are idle.
This help you to user available all times.
Second solution for your problem if you are not using any service for making user online. Then do that thing.
Here i made example of one to one chat and it available on github.
thanks hope this help you.
Depends on your XMPP server implementation, you can simply send message to yourself and check if you receive it.
Another way would be to perform XMPP service discovery, fetch your roster list etc. in short - do something that requires client-server communication.

How To Create Service In Android Which Makes Persist Xmpp Connection With XMPP Server?

After searching a lot on Internet we have came to one conclusion in order to ensure an persist connection with XMPP server we have to create a service,
We have created one which uses Smack library to connect with XMPP server and it is working fine with mobile and wi-fi network.
Every time you make something design approach always matter!!!, Smack have this reconnection mechanism already implicitly implemented in there library which listen to connection and if connection drops Smack try to reconnect with XMPP server at some interval of time.
Our use case scenario::
INTERNET connectivity can be because of wifi or data network,here if connection go is idle state of someone turn off screen cpu goes to sleep now any data is sent to server on this connection there will be no response because server is no more listening to client ,on client side XMPP connection is already in connected mode connection listener is not detecting any disconnection from server,so here flow gets completed.
After searching on INTERNET we found that possible solution to solve this is to ping server after a fix (we are using 1 min as fix period),after ping fail detected ,we have implemented reconnection mechanism same as smack(idea taken from Smack reconnection mechanism itself)by making use of timer task.
Problem:: only problem we have is battery draining ,if user is still connected with INTERNET and reconnection interval increases it will drain batty.
1). What is the possible solution of above problem?
2). Should we have to take another approach?
How To Create Service In Android Which Makes Persist Xmpp Connection
With XMPP Server?
Two things
Reestablish the connection, by listening for CONNECTIVITY_CHANGED intents and determine if the currently used data connection went down (and was replaced by another).
Ensure that the connection is established by pinging the server
Remarks about
Listening for CONNECTIVY_CHANGED is not enough, you need to compare the previously active connection with the now active one. And if it's not the same, re-establish the XMPP connection.
Smack 4.1 comes with ServerPingWithAlarmManager, which will check if a ping is required according to the settings of PingManager every 30 mintues. This value is hardcoded an can not be changed.
Using 1 minute as Ping interval is way to much! As you have experienced, it will drain you battery very fast. A reasonable ping interval is something > 15 minutes and I recommend 30 minutes. Smack 4.1 will also ensure that a ping is only send if there was no received stanza withing the Ping interval.
Also use XEP-0198: Stream Management when possible.
I recommend looking at the various open source apps that follow these guidelines and achieve a stable, permanent connection without draining the users battery1.
1: Just following these advises can not guarantee that the battery will drained. There are more factors to take into consideration.

Reuse asmack connection or recreate it in case of disconnect ( including broken TCP connections) in a safe way?

I'm coding a chat app, and I'm using asmack client library ( thank you Flow for maintain it :) ). The problem is I don't know how to handle disconnects ( normals, that are notified by connection listeners or broken TCP's caused ones )
Let's say I create the connection on thread A, that is I call connect & login on the same thread.
If, for any reason I will be disconnected, then next time, should I:
reuse the same XMPPConnection reference declared as volatile, and call connect on it on another thread? ( since Android doesn't let me call IO on main thread?). I don't really like this idea because most of the variables from XMPPConnection are not volatile, so calling connect on the same XMPPConnection from multiple threads ( one at a time, so after each disconnect I recall connect on a new thread ) could be problematic regarding thread safety.
recreate the XMPPConnection and clean-up the old one? Here is also a catch because while transition to the second connection, you could lose some messages. I am thinking of using a queue to hold my messages and write them as soon as a connection is available.
I know XMPP provides ping to detect if the server is still around, but if you do a ping at 1 min, but the connection becomes broken ( you remove the LAN cable from the wireless router to which you're connected with the phone, then Android will not notify you of connection lost, and you can still send messages on the socket for a time ), how can you achieve message sent integrity?
I am thinking to send some messages (ping ) synchronous, something like getRoster is implemented in XMPPConnection. That way if I don't receive a message after a timeout ( default in smack is 5 seconds ), then I will disconnect from XMPP, assuming my connection is broken. Do you think it's a good idea to rely on timeout or I can get screwed on mobile data connection?
The app that I want to make has WhatsApp's style, so it should work offline and resend messages when it relogins. If you have some recommendations about it, please share.
Many thanks,
If you want to stay connected all the time after established connection first time then i think you could try to maintain your connection in Service class of android.
The app that I want to make has WhatsApp's style, so it should work
offline and resend messages when it relogins
You need to make one Broadcast receiver which monitor when your data connection goes off and on .According that event you need to re-establish xmpp connection again (in other words start that service again )
Morevere , that was some brief but its yet lot more to manage in your case .I suggest one of my favorite opensource application GTalkSMS which may help you to understand how to deal with xmpp connection .

iOS notification when connection is back

I'm developing a queue of messages for a messenger app.
On Android if the connection is lost, messages is save on the queue and as soon as the 3G or wifi is back the messages go out of the queue. This is possible thanks I'm listening a broadcast receiver which alert me when the connection is back.
Is there anything similar on iOS?
Any workaround on iOS to achieve this behaviour?
You determine the restoration of an internet connection via the Reachability class. You can attempt resending once Reachability tells you that you have an Internet connection again.

How to monitor internet connection loss in android service

I want to write an android service at is activated when the internet connection goes down, and displays another activity to user when that happens. I don't know how to start to write this service. Can any one give me an idea or better tutorial how to catch the connection down within Android service.
Thanks
When Internet connectivity changes, the system sends out a broadcast with an android.net.conn.CONNECTIVITY_CHANGE intent. You should be able to create a broadcast receiver that will be called whenever the device is connected or loses its connection.
You've to write a receiver for the connection changes.
You can look right here : Android, How to handle change in network (from GPRS to Wi-fi and vice-versa) while polling for data

Categories

Resources