XMPPTCPConnection.PacketWriter

Hi there,

I recently started to use Smack library to work on a chat app, and so far is been really good, the documentation is clear.

So now I have a question and I cannot find anything in the docs to fix this issue that I have;

I am trying to handle the errors for send messages in my own way, so the current behavior is: I try to send a message with no internet connection, and it will be queued until the connection is back, and it will retry that by itself, which is fine, but for some messages, they are just simply lost, so what I want to do, is to find a way to clear the queue once the connection is lost, so I will have my messages in the database, and I will retry them manually, so that way I will avoid to lose some of the messages with the current behavior.

Looking into the code, I found that inside XMPPTCPConnection, there is this class PacketWriter, where there’s a queue, where we have all the elements, so I wonder if there is a way to clear the queue once the connection is gone, or if there is a better and easy way to perform that action, which is basically just avoid to retry to send the messages with a reconnection, I want to do it manually.

I hope you can help me.

Haven’t had a detailed look at the code, but I wonder if the queue should be emptied on a unclean disconnect without SM resumption being enabled. Then you don’t have to do it manually.

you’re right, the queue should be clear if is disconnected, but I think more important than that, should be configurable, because I think there should be some clients that want the api to retry the message by itself, in my case, I want to do that using the user’s feedback.

so I found a way to do that by myself, but is has nothing to do with the library, it is more about the logic;

before sending a message, I will ask if I am connected to the server, if I am connected, then will call the chat.sendMessage(message);

for now, it is working perfect as I want, but don’t know if there is a configuration already implemented to do that.

for now, it is working perfect as I want, but don’t know if there is a configuration already implemented to do that.
Yes, there is. sendStanza() will throw a NotConnectedException if not connected.

you’re right, the queue should be clear if is disconnected,
After thinking more about it and remembering the details: No, it should not. The idea is that you should use XMPP with XEP-0198: Stream Management (SM) and make use of Smack’s Stanza(Id)AcknowledgedListeners, which will tell you when a stanza was ack’ed by the server. Furthermore Smack will resend Stanzas after the connection got re-established even if SM resumption failed. The only case where Smack does not re-send the messages, is, if there is a StreamError. And of course, if Android kills your app/component.

So the strategy could be simple when using SM: Add the messages to a database in persistent storage which keeps track of the unacked (message) Stanzas, and re-send the those if the app was killed or a stream error happened.

1 Like

Thanks for the help, using the AcknowledgedListener to know if the server got the message is going to fix my problem, what I wanted to know is which of those messages were received for the server, so I can retry in other time manually, actually I have my messages in a local database in my device, so I can track the state of my message (pending/sent/failed).