Using node-xmpp to show a user as online through Adium

1.5k views Asked by At

I am running a local ejabberd 2 server and I have 2 users. [email protected] and [email protected]. Both users have each other on their rosters added from the ejabberd admin webportal.

I'm using adium on OSX logged in with the jeremy account. I would like to use the node console and see the billy account show as online allowing me to message billy from adium, and message jeremy from the node REPL.

var Client = require('node-xmpp-client'),
    ltx = require('node-xmpp-core').ltx;

var client = new Client({jid: '[email protected]', password: 'password'});

client.on('online', function(data) {
    console.log('Connected as ' + data.jid.user + '@' + data.jid.domain + '/' + data.jid.resource)
    var presence = new ltx.Element('presence', { type: 'available' }).c('show').t('chat');
    client.send(presence);
});

client.on('stanza', function(stanza) {
    console.log('RECEIVED STANZA', stanza);
});

client.on('offline', function () {
    console.log('Client is offline');
});

client.on('connect', function () {
    console.log('Client is connected');
});

// log in
client.connect();

var message = new ltx.Element('message',{to: '[email protected]', type: 'chat'}).c('body').t('sup?');

// send message
client.send(message);

var roster = new ltx.Element('iq',{id: 'roster_0',type: 'get'}).c('query', { xmlns: 'jabber:iq:roster'});

// get roster
client.send(roster);

// log out
client.end();

I can send messages from the billy account to the jeremy account, and vice versa just fine. However, I can't see that billy is online from Adium though.

To double test this, I created a third user called "asterisk". I added "asterisk" to the jeremy roster, and then set this user up through Asterisk. I can see this user online through Adium.

Any thoughts on what I might be missing?

1

There are 1 answers

0
jeremywoertink On BEST ANSWER

Ok, after some digging I got it working. I came across Simple XMPP which wraps the node-xmpp and got it all working.

The issue that I had was that my billy user didn't accept the subscription or subscribe to the jeremy user. Here's the whole thing

xmpp.connect({jid: '[email protected]', password: 'password', host: 'pbx.dev',port: 5222});
xmpp.on('online', function(data) { 
  xmpp.acceptSubscription('[email protected]'); 
  xmpp.subscribe('[email protected]');
  xmpp.getRoster();
});

xmpp.on('stanza', function(stanza) {
  var rosterRequest = stanza.name == 'iq' && stanza.attrs.type == 'result' && stanza.attrs.id == 'roster_0'
  if(rosterRequest) {
    var buddies = stanza.children[0].children;
    // do stuff with buddies
  }
});

// change status with
xmpp.setPresence('away', 'BRB AFK IRL');

I'd recommend digging through the simple xmpp code to learn a bit more. It's pretty small and really just wraps the node-xmpp.