Search code examples
javascripteventscallbackconverse.js

How to manage events in converse.js xmpp library


Im actually developing a custom ERP for my business company and i need implement xmpp web based chat applicatión using Converse.js.

But i can't find any solution to manage events callback's with the api.

According to the documentation, the following syntax should be used:

_converse.api.listen.on('message', function (messageXML) { ... });

OR

converse.api.listen.on('message', function (messageXML) { ... });

But "converse.api" it's undefinied

Converse.js version 6.0.1

I hope your valuable help.

Sorry for my bad english


Solution

  • Conerse.js has a public (i.e. globally accessible) API, which is available via the converse global. The most used method there would be converse.initialize.

    Most API methods are however restricted to the private (i.e. closured) _converse object (note the leading underscore) and is available via _converse.api.

    You can only access _converse.api if you register a plugin.

    For example:

    converse.plugins.add('myplugin', {
    
        initialize: function () {
            // This method gets called once converse.initialize has been called
            // and the plugin itself has been loaded.
    
            // Inside this method, you have access to the closured
            // _converse object as an attribute on "this".
            // E.g. this._converse
            const { api } = this._converse;
            api.listen.on('message', function (messageXML) { ... });
        }
    });