Skip to content

Instantly share code, notes, and snippets.

@arcusfelis
Last active January 1, 2016 04:49
Show Gist options
  • Save arcusfelis/8094612 to your computer and use it in GitHub Desktop.
Save arcusfelis/8094612 to your computer and use it in GitHub Desktop.
MAM with filtering messages by id

To limit amount of inspected messages we can use "start" and "end" elements.

<iq type='get'>
  <query xmlns='urn:xmpp:mam:tmp'>
      <start>2010-06-07T00:00:00Z</start>
      <end>2010-07-07T13:23:54Z</end>
  </query>
</iq>

After extending MAM, this code can be rewritten as:

<iq type='get'>
  <query xmlns='urn:xmpp:mam:tmp' from_id="XXX" to_id="YYY" />
</iq>

The result set contains messages with ids "XXX" and "YYY". The stanza can be rewritten to exclude them:

<iq type='get'>
  <query xmlns='urn:xmpp:mam:tmp' after_id="XXX" before_id="YYY" />
</iq>

To request recent messages you can use:

<iq type='get'>
  <query xmlns='urn:xmpp:mam:tmp'>
    <after>AAA</after>
    <max>50</max>
  </query>
</iq>

where "AAA" is a last known message id.

Result IQ:

<iq type='result'>
  <query xmlns='urn:xmpp:mam:tmp'>
    <set xmlns='http://jabber.org/protocol/rsm'>
       <first index='980'>BBB</first>
       <last>ZZZ</last>
       <count>1000</count>
    </set>
  </query>
</iq>

To calculate index and count values we should go through all messages.

The same query can be rewritten as:

<iq type='get'>
  <query xmlns='urn:xmpp:mam:tmp' after_id="AAA">
    <max>50</max>
  </query>
</iq>

Result IQ:

<iq type='result'>
  <query xmlns='urn:xmpp:mam:tmp'>
    <set xmlns='http://jabber.org/protocol/rsm'>
       <first index='0'>BBB</first>
       <last>ZZZ</last>
       <count>20</count>
    </set>
  </query>
</iq>

In this case index is always "0" and the count element contains the number of messages after "AAA". It can be bigger, than the page size.

Also the client can request amount of new messages without extracting them:

<iq type='get'>
  <query xmlns='urn:xmpp:mam:tmp' after_id="AAA">
    <max>0</max>
  </query>
</iq>
<iq type='result'>
  <query xmlns='urn:xmpp:mam:tmp'>
    <set xmlns='http://jabber.org/protocol/rsm'>
       <count>20</count>
    </set>
  </query>
</iq>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment