Node -xmpp send data from one client to another

I am using node-XMPP for a chat application where two clients can chat with each other. but the problem is i am receiving data from one client to server but i dont know how to send data from server to another client.
my server code:

'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:

   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 send data from other client data, it arrives at the server, but the server does not send data to the client.
Thanks in advance.

+3


source to share


2 answers


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


source


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 send the stanza to the same client it comes from. send it to the correct recipient client.
I have already answered this here. Please contact

Node xmpp messages are not sent to the correct destination

0


source







All Articles