Search code examples
javascriptstrophe

Strophe MAM how to display the messages?


I have managed to use Strophe MAM to get the archived messages into the RAWInput, and display the last message(but only the last one). How do i display all the messages from the RAWInput?? But not just the last one?

And how do i extract who the message is from?

I have limited the messages to the last 5.

connection.mam.query("test3@macbook-pro.local", {
  "with": "test4@macbook-pro.local","before": '',"max":"5",
  onMessage: function(message) {


            console.log( $(message).text());




  },
  onComplete: function(response) {
            console.log("Got all the messages");

  }
    });

Solution

  • You can get all the messages using the `strophe.mam.js plugin

    Here is my working code:

    // Retrives the messages between two particular users.
    
    var archive = [];
    
    var q = {
        onMessage: function(message) {
            try {
                var id = message.querySelector('result').getAttribute('id');
                var fwd = message.querySelector('forwarded');
                var d = fwd.querySelector('delay').getAttribute('stamp');
                var msg = fwd.querySelector('message');
                var msg_data = {
                    id:id,
                    with: Strophe.getBareJidFromJid(msg.getAttribute('to')),
                    timestamp: (new Date(d)),
                    timestamp_orig: d,
                    from: Strophe.getBareJidFromJid(msg.getAttribute('from')),
                    to: Strophe.getBareJidFromJid(msg.getAttribute('to')),
                    type: msg.getAttribute('type'),
                    body: msg.getAttribute('body'),
                    message: Strophe.getText(msg.getElementsByTagName('body')[0])
                };
                archive.val(archive.val() + msg_data.from + ":" + msg_data.message + "\n" + msg_data.to + ":" + msg_data.message + "\n");
                archive.scrollTop(archive[0].scrollHeight - archive.height());
                console.log('xmpp.history.message',msg_data.message);
            } catch(err){
                if(typeof(err) == 'TypeError'){
                    try {
                        console.log(err.stack)
                    } catch(err2){
                        console.log(err,err2);
                    }
                }
            }
            return true;
        },
        onComplete: function(response) {
            console.log('xmpp.history.end',{query:q, response:response});
        }
    };
    
    $(document).ready(function)(){
    archive = $("#archive-messages");
    archive.val("");
    $("#to-jid").change(function() {
                $("#archive-messages").val("");
                var to = {"with": $(this).val()};
                $.extend(q, to, before, max);
                // It takes around 800ms to auto login. So after this timeout we have to retrieve the messages of particular User.
                setTimeout(function(){
                connection.mam.query(Strophe.getBareJidFromJid(connection.jid), q);
            }, 1000);
            });
    });