Connecting to Google Talk over XMPP on Node.js

为君一笑 提交于 2019-11-29 20:31:24

Figured it out.

I was working with some inaccurate examples.

In my example above, you'll want to change:

var conn = new xmpp.Component({...})

...to...

var conn = new xmpp.Client({...})

I am on ubuntu linux so to install it I first had to do this(First install node/npm following receipe from npm website).

sudo apt-get install libexpat1 libexpat1-dev 
npm install node-xmpp
sudo apt-get install libicu-dev 
npm install node-stringprep

With this snippet I succesfully logged in and sent a message from my gmail account to my jabber.org account:

var argv = process.argv;
const xmpp = require('node-xmpp');
const sys = require('sys');

if (argv.length != 5) {
    sys.puts('Usage: node xmpp.js <my-jid> <my-password> <to>');
    process.exit(1);
}

const jid = argv[2];
const password = argv[3];
const to = argv[4];

// Establish a connection
const conn = new xmpp.Client({
    jid         : jid,
    password    : password,
    host        : 'talk.google.com',
    port        : 5222
});

conn.on('online', function(){
    console.log('online');

    conn.send(new xmpp.Element('presence'));
    conn.send(new xmpp.Element('message',
        { to: to, // to
            type: 'chat'}).
            c('body').
            t('testje'));
});

conn.on('error', function(e) {
    sys.puts(e);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!