node-xmpp send data from One client to another

1.2k views Asked by At

I'm using node-XMPP for a chat application where two clients can chat with each other. but the problem is I'm getting data from one client to the server, but I don't know how to send data from the server to another client.
my server code is:

'use strict';
    var xmpp = require('../index')
      , c2s = null
      , Client = require('node-xmpp-client')
      , ltx = require('node-xmpp-core').ltx
      , util = require('util');

    var startServer = function(done) {
        c2s = new xmpp.C2SServer({
            port: 5222,
            domain: 'localhost',
            preferred: 'PLAIN'
        })
        c2s.on('connect', function(client) {
           c2s.on('register', function(opts, cb) {
                console.log("REGISTER");
                cb(true);
            });
            client.on('authenticate', function(opts, cb) {
                if ('secret' === opts.password) {
                    return cb(null, opts)
                }
                console.log('FAIL')
                cb(false)
            });

            client.on('online', function() {
                console.log('ONLINE')
            });
            client.on('stanza', function(stanza) {
                console.log('STANZA', stanza.root().toString())
                 var from = stanza.attrs.from
                 stanza.attrs.from = stanza.attrs.to
                 stanza.attrs.to = from
                 client.send(stanza)
            });
            client.on('disconnect', function(jid) {
               console.log("client DISCONNECT=>");
            });
        });
         if (done) done()
    };

    startServer(function() {})

my client code is:

   var Client = require('../index.js')
        ,util = require('util');

    var client = new Client({
        jid: process.argv[2],
        password: process.argv[3],
        host :"localhost",
        port:5222,
        reconnect :true
    });

    client.on('online', function() {
        console.log('online');
    });
    client.on('error',function(err){
        console.log("error=>"+err);
    });

    client.on('stanza', function(stanza) {
        console.log('Incoming stanza: ', stanza.toString())
    });

    client.on('disconnect',function(){
        console.log("server diconnected");
    });

when I'm sending data from another client data come to the server but server Is not sending the data to the client.
Thanks in advance.

2

There are 2 answers

0
marian On
client.on('online', function() {
console.log("online");
var stanza = new xmpp.Element('message', { to: 'admin@localhost', type: 'chat', 'xml:lang': 'ko' }).c('body').t('aaaaaMessage from admin1');
//setInterval(sender,1000);

client.send(stanza);
console.log(stanza);

});

0
user1361425 On
client.on('stanza', function(stanza) {
            console.log('STANZA', stanza.root().toString())
             var from = stanza.attrs.from
             stanza.attrs.from = stanza.attrs.to
             stanza.attrs.to = from
      --->   client.send(stanza)

});

u are sending the stanza to the same client from which it is coming. send it to the correct receiver client.
I already answered it here. Please refer

Node xmpp messages are not sent to correct destination