Send XMPP notification from Node.js script
Asked Answered
F

1

5

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 simpler solution?

Facultative answered 11/12, 2017 at 21:50 Comment(6)
which xmpp server are you using?Vannavannatta
@divsingh: any XMPP server that can send a message to a Google Hangouts user.Facultative
I would suggest that chose an XMPP server that gives you a rest API to send stanza to a particular recipient. See ejabberd administrative API documentation docs.ejabberd.im/developer/ejabberd-api/admin-apiVannavannatta
as far as I know Hangouts does not support xmpp, while Google Talk service is being shut down. do you have other information?Cortez
however I tried with a node.js script and talk.google.com is still accepting XMPP connectionsCortez
@beaver: as long as Pidgin lists Google Talk, I'm fine.Facultative
C
7

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 = '[email protected]';
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('[email protected]', '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('[email protected]');

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 = '[email protected]';
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.

Cortez answered 4/1, 2018 at 14:25 Comment(5)
I tried with two different <user>@gmail.com accounts, which are both using Hangouts fine, and got XMPP authentication failure each time. I had enabled less secure apps and set the server to talk.google.com. Thoughts?Facultative
I also tried with different accounts and it worked; did you use the minimal script I suggested?Cortez
In addition, if the two have never spoken together, the 'subscribe' is also requiredCortez
Never mind, it was a suspicious IP issue. Saw it after I logged in as the sender - there was a "Warning: Google prevented a suspicious attempt to sign in to your account using your password.". Now the script connects, but hangs at xmpp.send.Facultative
I'm using the simple script and the users had talked via hangouts.google.com. Turns out I had to run subscribe (once) anyway. Glad we solved it, and thanks for the answer!Facultative

© 2022 - 2024 — McMap. All rights reserved.