9

I'm using an XMPP server that implements XEP-0313 for retrieving conversation history. I would like to fetch only the last message of each conversation, so that I can build a list of your most recent conversations previewing the last message.

I've managed to fetch all messages of all conversations and based on that I could build the list, but it's a big waste of data and not an option. I'm not sure this is the right extension for accomplishing this, so if there is another extension I should be looking at, please guide me in the right direction.

Erik B
  • 40,889
  • 25
  • 119
  • 135
  • I am developing an android chat app with xmpp. I have successfully implemented that and i am able to send and receive messages. I have stored the chat data internally using SQLite on the device but the chat data is deleted once the app is uninstalled. So i checked about the XEP - 0313 which can save the chat beween the users on the server, and i am having issues with retrieving chat history between two users from the eJabberd server. I have read about the XEP - 0313 but i don't know how i can use that in my android app. I am using Android Studio. Thanks – Paritosh Chaudhary Dec 27 '16 at 05:59
  • @Paritosh I don't see how this has anything to do with my question. Seems like you just want my help implementing XEP-0313. I did this for iOS, so I am not sure what libraries are available for Android, but for iOS I couldn't find an implementation of this protocol at the time, so I had to implement it myself. It was a lot of work, but I can't say that it was difficult. It's just normal development work. If you need someone else to do your development work, you should hire someone. I am currently not available, but if you only need help to get started, I suppose I could find the time. – Erik B Jan 03 '17 at 16:25
  • Hi..!! Thanks for replying. I have successfully implemented the XEP - 0313 in Android now. I just wanted to know how we can implement that library in Android as the methods to fetch the history should be similar as we would be using the same library in Android and iOS. I used MamManger at my end to retrieve the history. – Paritosh Chaudhary Jan 04 '17 at 05:52
  • @ErikB did you find a proper solution? I'm asking the similar here https://stackoverflow.com/questions/52085402/how-to-implement-a-list-of-chats-with-xmpp but still no god advises – Rubycon Aug 31 '18 at 09:31

1 Answers1

6

One thing you can do easily is first retrieve the user's roster and then for each contact retrieve the latest message.

<iq from='juliet@example.com/balcony'
    id='bv1bs71f'
    type='get'>
  <query xmlns='jabber:iq:roster'/>
</iq>

Result:

<iq id='bv1bs71f'
    to='juliet@example.com/chamber'
    type='result'>
   <query xmlns='jabber:iq:roster' ver='ver7'>
     <item jid='nurse@example.com'/>
     <item jid='romeo@example.net'/>
   </query>
 </iq>

Retrieve the last message from or to nurse@example.com:

<iq type='set' id='juliet1'>
  <query xmlns='urn:xmpp:mam:1'>
    <x xmlns='jabber:x:data' type='submit'>
      <field var='FORM_TYPE' type='hidden'>
        <value>urn:xmpp:mam:1</value>
      </field>
      <field var='with'>
        <value>nurse@example.com</value>
      </field>
    </x>
    <set xmlns='http://jabber.org/protocol/rsm'>
      <max>1</max>
      <before/>
    </set>
  </query>
</iq>

Of course users can have conversations with people not on their roster, but in practice this is quite rare on XMPP.

xnyhps
  • 3,306
  • 17
  • 21
  • My experience with XMPP is very limited, but from my understanding, the roster is your "contact list". I'm assuming that it wouldn't include group chats, right? Meaning that if you participate in a group chat it would not show up in the conversation history using this technique, which isn't good enough for me. Please correct me if my assumption is wrong. – Erik B Feb 08 '16 at 17:48
  • Correct, your roster is your contact list. It appears to me that most server implementations don't archive group chat messages anyway, but instead assume the group chat servers expose a MAM archive themselves. That means it's easy to obtain the last message, but it will be much harder to discover which group chats the user has been in unless they are all bookmarked. – xnyhps Feb 08 '16 at 18:29
  • It seems to me like I might need a non-standard XMPP-extension to accomplish the task. I find that a bit surprising considering that my use case must be quite common and XMPP is the dominating standard. Someone must have done this before me. – Erik B Feb 09 '16 at 11:50
  • @ErikB There's been some progress lately on writing a new group chat protocol called MIX: https://xmpp.org/extensions/xep-0369.html. I think your use-case would be much easier with MIX. – xnyhps Feb 09 '16 at 12:40
  • @xnyhps If you do this you'll have to make a network call for each member of your roster. If I have 1000 people on my roster and each network call takes 250ms that 25 seconds plus a ton of network use. Is there any way to do this in bulk? – Zachary Sweigart Aug 16 '18 at 02:52
  • @ZacharySweigart You don't need to wait 250ms for the results before sending the next query, you can send multiple packets at the same time. It's indeed still a lot of traffic, though. – xnyhps Aug 17 '18 at 05:00
  • I had a socket connection working at the same time in NODEJS to fetch the latest chat from DB, I pass in the JIDs to receive the latest chat for each user. – Gagan Babber Jul 23 '20 at 12:12
  • @xnyhps even after im passing 1 not getting only 1 last message its still give all the messages which are delivered in that user , can you pls guild me how to pass element here ? or your swift code to send this iq on server side to fetch teh messages ? – Akash Thakkar Jan 27 '21 at 13:17