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

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.

Related

Android Real-Time application

I'm working on app the share tasks between users. By Google Cloud Messaging, I can notify the user target that he has a new task shared. The problem is : GCM does not offer a delivery guarantee. Would someone use an app like WhatsApp if he took minutes to deliver a message or not come to hand? That's my problem with GCM.
So I've got a solution : Use Socket!!!
Using Socket.oi and Node.js, my dream came become really works like magic !!!
But as nothing is free, keep a socket connection has a very high cost for the battery. Some people argue that the use of Sockets when there is no communication, nothing in or out, no cycles, so there is no consumption.
My friends, I've read a lot of text and do not know what approach should I follow. I ask your help. Soket.oi? WebSocket ???
How to maintain a connection to my server permante preserving the most of the battery?
I appreciate everyone's help!
You need to use mix of a socket connection and GCM. Both connection types do no guarantee delivery so you need to implement mechanism which checks consistency of messages history.
Simplified scenario could look like this:
a user launches your client
the client app registers at GCM and sends google id to your server.
the client app establishes socket connection
your server sends messages to a client through GCM and socket connection (if it is establish with particular connection)
each message has unique id, therefore the client could just ignore second identical message from Google of a socket connection
About not delivered messages:
When client connects to the server through socket connection it should receive response where history of messages should be put. It shouldn't be full history, it could be just last message (in case you develop chat app). Then a client just checks if he has notified user about last message or not. If not then your client makes request(http or through socket) to your server and receives undelivered messages.
Battery consumption:
Do not acquire wake lock to maintain socket connection! A device must go sleep. GCM will wake up handset.
Socket.io is good, and certainly useful in many real-time applications, but what happens when the app is terminated by the user? Or if the user restarts their phone? How would you receive notifications then?
For all purposes, GCM is good enough.

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 .

Android client tell Syn & receive Rst Ack

I'm working for an App Android and I need my phone was always connected to my server MQTT...
But my phone can lost his connection(Tel :[FIN;ACK]; Serv:[FIN;ACK];Tel:[ACK]); after that, when he send a [SYN] message for connect it to the server MQTT, the serveur send an [RST,ACK]... And this while the service of my app is in activity.
In my case , i MUST to re-launch my server if i want my phone can be reconnect, but, i can't reboot my server (multi-phone possible, and i need real-time performence).
There is there a possibility to connect my phone without reboot my server?
Thank's for time.
Best regard
Guillaume
I think your problem is similar to the one I had. The connection was half closed. See http://www.codeproject.com/Articles/37490/Detection-of-Half-Open-Dropped-TCP-IP-Socket-Conne for details.
The way I solved this was to i) add keep alive msgs, every 5 secs. from the phone to the server and ii) have the server close the connection if there was no incomming data for 15sec. See http://www.forward.com.au/pfod/CheapWifiShield/index.html and www.pfod.com.au
MQTT includes keepAlives (PING request msgs) so you only need to modify your client to specify a short keepAlive interval
see http://www.hivemq.com/blog/mqtt-essentials-part-10-alive-client-take-over
to have the MQTT server to more quickly close the connection once it stops getting your data or keepAlive msgs.
Although the above link states
"The broker will close the previous connection to the same client (determined by the same client identifier) and establishes the connection with the newly connected client. This behavior makes sure that half-open connection won’t stand in the way of a new connection establishment of the same client."
So you should not be having this problem at all !!!

Is a live internet connection required for push notification?

This maybe a silly question, but I need confirmation and I have no one else to ask.
I am trying to understand the implications of implementing my own push notification for android mobile devices. This requires a continuous TCP connection to a server, though most of the time it will be idle.
My assumption is that, even when idle, for the server to be able to push data to the client through the TCP connection, an active internet connection will always be required, and if the connection is disrupted (i.e. the user switches the connection off) this push will no longer be possible.
Is this assumption correct ?
I'd say yes, it's correct indeed. How would you communicate without a live internet connection?
Afaik, Android C2DM handles this by queueing up the push requests when the user does not have a live net connection and sending them down to the user when the connection gets back up. You could implement a similar behavior in your solution's server side.
Answer is YES
To understand the best way then any-other about C2DM, You just should go through this Google Project C2DM
They have provided complete documentation required to Understand the work and mechanism of C2DM and provided completed Examples too.
I have one another favourite tutorial for the same is : Vogella's
You just need to give a good time to read this thing and implement..
Happy Coding :)
You are right. C2DM maintains a open socket (with Market or Gmail app), which it uses to identify your device.
And ofcourse, you will need Wifi or a cellular network to receive the push notifications.

Categories

Resources