Send XMPP notification from Node.js script

前端 未结 1 774
星月不相逢
星月不相逢 2021-01-06 00:29

How can a Node script send a notification via XMPP to a Jabber user (e.g. via Google Hangouts)? I\'ve looked at libraries like xmpp/client but they seem overkill. Is there a

相关标签:
1条回答
  • 2021-01-06 01:10

    Simplest way to send a message via XMPP in Node

    There is probably no other simpler XMPP client library for Node than node-simple-xmpp. In this case the minimal Node.js script to send a message to another Jabber user is:

    var xmpp = require('simple-xmpp');
    
    var jid = 'testjs@xmpp.jp';
    var pwd = 'xyz';
    var server = 'xmpp.jp';
    var port = 5222;
    
    xmpp.on('online', function(data) {
        console.log('Connected with JID: ' + data.jid.user);
        xmpp.send('testjs@007jabber.com', 'hello! time is '+new Date(), false);
    });
    
    xmpp.on('error', function(err) {
        console.error("error:", JSON.stringify(err));
    });
    
    xmpp.connect({
        jid: jid,
        password: pwd,
        host: server,
        port: port
    });
    

    If the two account have never spoken together, a preliminary 'subscribe' is also required:

    xmpp.subscribe('testjs@007jabber.com');
    

    As you can see in package.json node-simple-xmpp lib has a dependency on [node-xmpp-client] (https://github.com/xmppjs/xmpp.js/tree/node-xmpp/packages/node-xmpp-client).

    Usage with Google Talk/Hangouts

    The script above is working (tested) also with Google Talk/Hangouts, you just have to replace xmpp.jpserver with talk.google.com and use a Google account. Turn on https://myaccount.google.com/lesssecureapps to enable Node.js script to sign in with Google account.

    Other XMPP libraries

    As of https://npms.io/search?q=node-xmpp there are a few other XMPP Client libraries for Node, however almost all of them are dependent on node-xmpp-client or limited to BOSH connection (polling over HTTP).

    One interesting lib for those used to Strophe.js on client side seems node-strophe. It is based on Strophe.js release 1.0.2 which is a library for applications that run in any browser. Unfortunately that version didn't support other than BOSH (see Strophe.js changelog), websocket is available only since release 1.1.0.

    Exploring alternatives without specific XMPP libraries

    An alternative solution without specific XMPP libraries could be using Net module, but in this case you need to manage all XMPP interactions to establish the connection to the server, see https://wiki.xmpp.org/web/Programming_XMPP_Clients .

    Below is a very raw example of script trying to initiate the connection with a Jabber server using Net module:

    var net = require('net');
    
    var jid = 'testjs@xmpp.jp';
    var pwd = 'xyz';
    var server = 'xmpp.jp';
    var port = 5222;
    
    var msg = '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0" to="'+server+'">';
    
    var client = new net.Socket();
    client.connect(port, server, function() {
        console.log('Connected');
        client.write(msg);
    });
    
    client.on('data', function(data) {
        console.log('Received: ' + data);
    });
    

    You can see in the console log the correct answer of Jabber server, however from then on it's a mess: you should begin exchanging TLS messages (see https://xmpp.org/rfcs/rfc3920.html#tls)

    Conclusions

    I think the only feasible alternative is the first one using node-simple-xmpp library.

    0 讨论(0)
提交回复
热议问题