javascriptnode.jsxmppcisconode-xmpp

Reading XMPP console using node-xmpp


I have an issue at work and I can't find a solution. I know StackOverflow questions aren't for documentation, but apparently the documentation for node-xmpp is rather... "light".

My problem is the following:

I have an xmpp client connected to a server. This server is used to send notifications to my client when it's REST API is called. Basically the HTTP Request will get a 202 code if the server determines the request looks OK and will return the full XML answer through XMPP protocol.

For those who may have some insight on this specific matter: I am trying to use Cisco Finesse API Notifications system.

I managed to view those notifications using Pidgin, but the only way to see them is to open the XMPP console (and not the regular chat flow).

Now I managed to log in to this XMPP server using node-xmpp but I just can't read notifications. I tried to listen on "data" and "stanza" events, but it never triggers those events.

Here is the code excerpt:

const XMPP = require("node-xmpp-client");

let xmpp_client = new XMPP({
    jid: json_message.login + '@abc.inc',
    password: json_message.pwd,
    host: CONF.FINESSE_SERVER.replace("http://", ""),
    port: 5222
});

console.log("jid: " + json_message.login + '@abc.inc');
console.log("password: " + json_message.pwd);
console.log("host: " + CONF.FINESSE_SERVER.replace("http://", ""));
console.log("post: 5222");

xmpp_client.on("online", function(){

    console.log("online");

});

xmpp_client.on('stanza', function(stanza) {

    console.log("xmpp::");
    console.log('Incoming stanza: ');
    console.log(stanza);

});

xmpp_client.connection.on("data", function(data) {

    console.log(data.toString("utf-8"));

});

I am a bit confused right now because the documentation from Cisco doesn't detail anything about JavaScript and the node-xmpp documentation is super short.

Any help is welcome, thanks in advance people! \o/


Solution

  • You need to send a presence stanza and also subscribe to the events you'd like to receive. Example:

    const xmppXml      = require('@xmpp/xml')
        , xmppClient   = require('node-xmpp-client')
        ;
    
    let host = YOUR_HOST;
    let fqdn = YOUR_FQDN;
    let username = YOUR_USERNAME;
    let password = YOUR_PASSWORD;
    let jid = username + '@' + fqdn;
    
    let xmpp_client = new xmppClient({
        jid: jid,
        password: password,
        host: host,
        port: 5222
    });
    
    xmpp_client.connection.on('data', (data) => {
        console.log(data.toString('utf-8'));
    });
    
    xmpp_client.on('online', () => {
        xmpp_client.send(new xmppClient.Stanza('presence', { }));
    
        // Subscribe to REAL-TIME events.
        // REFERENCE: https://developer.cisco.com/media/finesseDevGuide/CFIN_RF_S7A50AC1_00_subscription-management.html
        let subscribe = xmppXml.createStanza('subscribe', {
            xmlns: 'http://jabber.org/protocol/pubsub',
            node : '/finesse/api/User/' + username,
            jid  : jid,
        });
        let pubsub = xmppXml.createStanza('pubsub', {
            xmlns: 'http://jabber.org/protocol/pubsub',
        });
        let iq = xmppXml.createStanza('iq', {
            to  : 'pubsub.' + fqdn,
            from: jid,
            type: 'set',
        });
        iq.cnode(pubsub).cnode(subscribe);
    
        let msg = iq.toString();
    
        xmpp_client.send(msg);
    });