Yashwanth Babu
Yashwanth Babu

Reputation: 939

ejabberd MAM doesn't work with Strophe js

I am trying to show the conversation between the two users even after the users logout and login. I mean when user1 logged out and again logged in, he should see the conversations made with the user2. I am using Ejabberd XMPP server and Strophe Js to retrive the messages.

As I found this strophe.mam.js plugin to do this but raising the error and cant get the messages.

Here is my code:

function onConnect(status)
    {
        // Functions runs while users trys to login to the XMPP server
        var iq = null;

        switch (status)
        {
            case Strophe.Status.CONNECTING:
                log('Connecting.');
                break;
            case Strophe.Status.CONNFAIL:
                log('Failed to connect.');
                $('#connect').get(0).value = 'connect';
                break;
            case Strophe.Status.DISCONNECTING:
                log('Disconnecting.');
                break;
            case Strophe.Status.DISCONNECTED:
                log('Disconnected.');
                $('#connect').get(0).value = 'connect';
                break;
            case Strophe.Status.CONNECTED:
                log('Connected.');
                connection.addHandler(onMessage, null, 'message', null, null,  null);
                connection.addHandler(onPresence, null, 'presence', null, null, null);

                iq = $iq({type: 'get'}).c('query', {xmlns: 'jabber:iq:roster'});
                connection.sendIQ(iq, onRoster);

                break;
            default:
                break;
        }
    }

function onMessage(msg) {
    debugger;
    var fromJid = msg.getAttribute("from"),
        bareFromJid = Strophe.getBareJidFromJid(fromJid),
        type = msg.getAttribute("type"),
        elems = msg.getElementsByTagName("body");

    if (type == "chat" && elems.length > 0) {
        var body = elems[0],
            message = Strophe.getText(body);

        showMessage(bareFromJid + ": " + message);
              connection.mam.query("yashwanth@localhost", {
  "with": bareFromJid,
  onMessage: function(message) {
            console.log("Message from " + bareFromJid,
                ": " + message);
            return true;
  },
  onComplete: function(response) {
            console.log("Got all the messages");
  }
    });
    }

    return true;
}

function send() {
    // Handles with sending the message
    var to = $('#to-jid').get(0).value,
        myBareJid = Strophe.getBareJidFromJid(connection.jid);
        message = $('#message').get(0).value,
        reply = $msg({to: to, type: 'chat'})
            .c("body")
            .t(message);

    connection.send(reply.tree());
    showMessage(myBareJid + ": " + message);
}

$(document).ready(function () {
    connection = new Strophe.Connection(BOSH_SERVICE);
    messagebox = $("#messages");
    messagebox.val("");
    logbox = $("#log-messages");
    logbox.val("");
    rosterbox = $("#roster");
    rosterbox.val("");
    connection.rawInput = function (data) { log('RECV: ' + data); };
    connection.rawOutput = function (data) { log('SEND: ' + data); };

    Strophe.log = function (level, msg) { log('LOG: ' + msg); };

    login();
    $('#send').bind('click', send);
});

So whenever the user receives the message there will be something in the console. But it returns me this error in my logs

RECV: <body xmlns='http://jabber.org/protocol/httpbind'><iq xmlns='jabber:client' 
from='yashwanth@localhost' to='yashwanth@localhost/22064184271436881211352579' 
id='yashwanth@localhost' type='error'><query xmlns='urn:xmpp:mam:0'><x xmlns='jabber:x:data'><field 
var='FORM_TYPE'><value>urn:xmpp:mam:0</value></field><field var='with'>
<value>shabda@localhost</value></field></x><set xmlns='http://jabber.org/protocol/rsm'/></query>
<error code='400' type='modify'><bad-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>
</iq></body>

Please help me out with this

Upvotes: 1

Views: 1942

Answers (2)

icaro56
icaro56

Reputation: 167

I had a problem with MAM. See this link: https://www.ejabberd.im/forum/25028/solved-how-configure-and-test-modmam-message-archive-management

I solved the problem change type 'set' to 'get'

Upvotes: 1

Micka&#235;l R&#233;mond
Micka&#235;l R&#233;mond

Reputation: 9055

Your MAM query is not correctly formatted.

You are missing the attribute type="submit" on the x element with xmlns jabber:x:data feature. Type is mandatory in XEP-0004 Data Forms

You IQ should be:

<iq type='set' id='juliet1'>
  <query xmlns='urn:xmpp:mam:0'>
    <x xmlns='jabber:x:data' type='submit'>
      <field var='FORM_TYPE' type='hidden'>
        <value>urn:xmpp:mam:0</value>
      </field>
      <field var='with'>
        <value>shabda@localhost</value>
      </field>
    </x>
    <set xmlns='http://jabber.org/protocol/rsm'/>
  </query>
</iq>

See Example 6 in XEP-0313 Message Archive Management.

The MAM strophe plugin had a bug. We prepared a fix here: https://github.com/processone/strophejs-plugins/commit/5a7857e2ab625c0521c68719d7e220f00c32c593

And submitted this pull request: https://github.com/strophe/strophejs-plugins/pull/65

Upvotes: 1

Related Questions