1

Is it possible to receive custom stanzas in converse.js?

I tried to listen to incoming messages:

converse.plugins.add('dummy', {
  initialize: function() {
    var _converse = this._converse;

    _converse.api.listen.on('message', function(xmlMessage) {
      console.log('Received message!');
    });
  }
});

My custom stanza looks like this:

<message to='...' from='...' type='groupchat'>
    <custom_stanza>
        <created_at>2018-02-14T16:25:00+01:00</created_at>
        <store xmlns='urn:xmpp:hints'/>
    </custom_stanza>
</message>

But unfortunately this stanza won't get recognized here. Normal messages work.

zarathustra
  • 1,898
  • 3
  • 18
  • 38

1 Answers1

1

I'm not sure why a message event isn't triggered for your custom message. There must be some assumption in the converse.js code which your custom message doesn't fulfill.

Converse.js uses Strophe.js under the hood, so you can use Strophe's addHandler to register an event handler on a lower level.

Here's how you would do this:

converse.plugins.add('dummy', {
  initialize: function() {
    var _converse = this._converse;

    _converse.on('connected', () => {

      // _converse.connection is an instance of Strophe.Connection
      // which provides the `addHandler` method.

      _converse.connection.addHandler((message) => {
         // Your message handling code comes here...

      }, null, 'message');
   });
  }
});

An example of this usecase is in the converse-bookmarks.js plugin.

JC Brand
  • 2,652
  • 18
  • 18
  • Thank you so much for your answer. I implemented a new handler with Strophe's addHandler - and now I am able to read custom stanzas. Unfortunately this does not work for archived messages. How can I read archived custom stanzas? – zarathustra Feb 20 '18 at 14:05
  • The same way, just make sure what you're registering your handler correctly. Read the Strophe.js API documentation for `addHandler` which I linked to in my answer. – JC Brand Feb 21 '18 at 14:46