How to send XMPP message when sender is offline?
Asked Answered
R

2

7

I am using aSmack and Openfire for my chat application. I am able send and receive message finely. Openfire support offline message transfer when recipient is offline by keeping message until he comes online.

But what to do when sender is offline or his internet drops between communication?

Is there any api provided by aSmack/Smack which keeps message until internet is back ?

Or should i send my messages through SQLite ?

Rafter answered 29/3, 2015 at 5:36 Comment(2)
Hello @Sushant, I'm facing to the same issue, any helpEelpout
@AT_AB i end up using shared preferences and sqlite. Store recipient name in shared preferences before sending message to him, then get all unsent messages from sqlite and send them to that recipient and after successful sent remove recipient from shared preferences.Rafter
R
2

Well answering after a while, but perhaps still it would point someone in right direction.

After searching for a while, i get to know that aSmack doesn't provide any offline storage.

To send message when sender is offline, we need offline storage to store message until sender gets back online. I used sqlite for this, but SharedPreferences would also be a good option if we have to delete message after successfull sending.

Best solution, which i follow is, insert message in sqlite, store it's id in shared preferences, send message and then remove id from shared preferences. In mean while if intenet connection drops or message couldn't send by any reason, then we have it's id in shared preferences as backup. After getting online, we can go through shared preferences to check if their is any message is pending to send.

Rafter answered 15/12, 2015 at 12:15 Comment(8)
I think you are talking about sender side only, what if receiver is offline and when receiver gets online does he/she receives messages during his/her offline period?Laellaertes
@Ahesanali Momin XMPP server hadles this case. It stores messages if recevier is offline and delivers them when receiver connects to server.Rafter
Thank you very much, You are right. XMPP server doing it if we enable this offline message plugin.Laellaertes
Hi @AhesanaliMomin : Can you please provide me the plugin name in which if user is offline under GROUP and connected to the internet he/she will get message instantly?Massorete
In openfire it listed as save offline message. Something like that.Laellaertes
If you are doing p2p messages at that time offline messages are stored into ofOffline table but when you are talking about group chat all the messages are stored into ofMessageArchive table itself with few seconds delay. @AhesanaliMomin is there any way that monitoring service plugin inserts data in faster manner? moreover can you please look into #37346841Overt
is there any way we can check if the message is send or not as newOutgoingMessage(to: EntityBareJid?, message: Message?, chat: Chat?) is always called whenever i send the message and no SmackException.NotConnectedException is thrownKyungkyushu
@Killer: I couldn't found any callback method which notifies us after successful send. So we got to check everything before we call send method and then mark that message as "sent" in sqlite.Rafter
R
2

If you are connected to openfire and internet goes off then you are still online on openfire because you can't change presence if internet is off.

For this openfire use http://xmpp.org/extensions/xep-0199.html

If user app doesn't reply ping request then openfire makes it offline and offline storage starts.

For getting offline messages in asmack you need to add following providers.After adding these you will get offline messages if they are enabled from the server

pm.addIQProvider("offline", "http://jabber.org/protocol/offline",
                new OfflineMessageRequest.Provider());
        // Offline Message Indicator
        pm.addExtensionProvider("offline",
                "http://jabber.org/protocol/offline",
                new OfflineMessageInfo.Provider());

enter image description hereions/xep-0199.html

Razid answered 29/3, 2015 at 15:13 Comment(3)
Thanks for your answer. But i am not asking for openfire offline storage. I am asking about aSmack offline storage. Is there any api for it ?Rafter
Edited the answer add offline providerRazid
Yes. I know we can get offline messages when reconnected to server. But if i am offline while sending the message, the message will get lost. What i want is keep the message until i reconnect to network and send when network is back.Rafter
R
2

Well answering after a while, but perhaps still it would point someone in right direction.

After searching for a while, i get to know that aSmack doesn't provide any offline storage.

To send message when sender is offline, we need offline storage to store message until sender gets back online. I used sqlite for this, but SharedPreferences would also be a good option if we have to delete message after successfull sending.

Best solution, which i follow is, insert message in sqlite, store it's id in shared preferences, send message and then remove id from shared preferences. In mean while if intenet connection drops or message couldn't send by any reason, then we have it's id in shared preferences as backup. After getting online, we can go through shared preferences to check if their is any message is pending to send.

Rafter answered 15/12, 2015 at 12:15 Comment(8)
I think you are talking about sender side only, what if receiver is offline and when receiver gets online does he/she receives messages during his/her offline period?Laellaertes
@Ahesanali Momin XMPP server hadles this case. It stores messages if recevier is offline and delivers them when receiver connects to server.Rafter
Thank you very much, You are right. XMPP server doing it if we enable this offline message plugin.Laellaertes
Hi @AhesanaliMomin : Can you please provide me the plugin name in which if user is offline under GROUP and connected to the internet he/she will get message instantly?Massorete
In openfire it listed as save offline message. Something like that.Laellaertes
If you are doing p2p messages at that time offline messages are stored into ofOffline table but when you are talking about group chat all the messages are stored into ofMessageArchive table itself with few seconds delay. @AhesanaliMomin is there any way that monitoring service plugin inserts data in faster manner? moreover can you please look into #37346841Overt
is there any way we can check if the message is send or not as newOutgoingMessage(to: EntityBareJid?, message: Message?, chat: Chat?) is always called whenever i send the message and no SmackException.NotConnectedException is thrownKyungkyushu
@Killer: I couldn't found any callback method which notifies us after successful send. So we got to check everything before we call send method and then mark that message as "sent" in sqlite.Rafter

© 2022 - 2024 — McMap. All rights reserved.