Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS xmpp server

Tags:

node.js

xmpp

I make the first steps in the node js and xmpp

I need to run at xmpp server on node js for messaging

Here's the process: I use node-xmpp server https://github.com/astro/node-xmpp run the example of a server (/examples/c2s.js) join to server with two clients (clients tested on other servers jabber - it works and the messages are sending through)

Clients have authorization on my server. But when I send a message from one client to another, the message comes to the server (I see it in the logs) and that was the message does not come to recipient

I don `t know where to look for the problem server configuration ? routing ? messaging may be necessary to add yourself ?

help me plz

my server code (by examples)

var xmpp = require('../lib/node-xmpp');

var c2s = new xmpp.C2SServer({
    port: 5222,
    domain: 'localhost'

});

// On Connect event. When a client connects.
c2s.on("connect", function(client) {
    c2s.on("register", function(opts, cb) {
        console.log("REGISTER");
    cb(true);
    });

    client.on("authenticate", function(opts, cb) {
        console.log("AUTH" + opts.jid + " -> " +opts.password); 
    cb(null); 
    });

    client.on("online", function() {
        console.log("ONLINE");
        client.send(new xmpp.Message({ type: 'chat' }).c('body').t("Hello there, little client."));
    });

    client.on("stanza", function(stanza) {
        console.log("STANZA" + stanza);

    });

    client.on("disconnect", function(client) {
        console.log("DISCONNECT");
    });

});

I run a server and connect to it by this code

var xmpp = require('../lib/node-xmpp');
var argv = process.argv;

if (argv.length < 6) {
    console.error('Usage: node send_message.js <my-jid> <my-password> <my-text> <jid1> [jid2] ... [jidN]');
    process.exit(1);
}

var cl = new xmpp.Client({ jid: argv[2], password: argv[3] });

cl.addListener('online',
   function() {argv.slice(5).forEach(
       function(to) {cl.send(new xmpp.Element('message', { to: to,type: 'chat'}).c('body').t(argv[4]));
       });

       // nodejs has nothing left to do and will exit
       // cl.end();
   });

cl.addListener('stanza',
    function(stanza) {
        if (stanza.is('message') &&
            // Important: never reply to errors!
            stanza.attrs.type !== 'error') {
            console.log("New message");
            // Swap addresses...
            stanza.attrs.to = stanza.attrs.from;
            delete stanza.attrs.from;
            // and send back.
            cl.send(stanza);
        }
    });

cl.addListener('error',
    function(e) {
       console.error(e);
       process.exit(1);
   });
like image 441
Alex Nechaev Avatar asked Sep 25 '13 09:09

Alex Nechaev


People also ask

What is XMPP node?

Definition. To clarify the nature of a node, it is first helpful to describe the architecture of XMPP systems. Because XMPP is a client-server technology that relies on the Domain Name System, the fundamental building block of XMPP systems is the "domain".

What is Jabber XMPP server?

Jabber/XMPP (Extensible Messaging and Presence Protocol formerly named Jabber) is an open-source, decentralized Instant Messaging protocol that DreamHost offers free of charge on Shared, VPS, and Dedicated Hosting plans. DreamPress does not offer free Jabber service.

How does XMPP protocol work?

As mentioned above, XMPP works by passing small, structured chunks of XML data between endpoints (clients) via intermediary servers. In other words, if you send a message to your friend using XMPP, that message, as part of an XML document, first travels to a server instead of traveling directly to your friend's device.


Video Answer


2 Answers

The short answer: change cb(null) to cb(null, opts).

The long answer:

  1. client.on("authenticate", function(opts, cb) {...}) registers what the server will do when the client tries to authenticate itself. Inside node-xmpp, it will look for the authentication mechanism first and the mechanism will then call the callback and retrieve the authentication results via cb.

  2. By default, the Plain authentication is used. You can check out how it works here: https://github.com/node-xmpp/node-xmpp-server/blob/master/lib/authentication/plain.js. With Plain the opts stores the jid and password.

  3. Then to inform node-xmpp that authentication failed/sucessed, we need to look into mechanism, https://github.com/node-xmpp/node-xmpp-server/blob/master/lib/authentication/mechanism.js, inherited by Plain. this.authenticate(this.extractSasl(auth), function (err, user) { if (!err && user) { self.success(user) } else { self.failure(err) } }) Here, cb requires two parameters. When err is null and user is non-null, it indicates authentication successes. Otherwise, failed.

like image 193
mingyuan-xia Avatar answered Oct 02 '22 02:10

mingyuan-xia


I am no expert on neither node.js nor xmpp. But reading your code. I assume the "stanza" is the event where a client sent a message. You asked it to log the message, but you gave no instructions on how to route it to the recipient. You should break down the received message on the server into message body and recipient, and ask your server to send it to the recipient.

like image 25
Joe Yahchouchi Avatar answered Oct 02 '22 02:10

Joe Yahchouchi