I'm using node-xmpp server as a chat server for 2 clients:Psi and Spark.I register an account for every client.I want to send from Psi account a message to Spark account.Every time when i send a message from an account the same account receive the message.I want to add friend to my list(i don't know why doesn'w work..probably is not implemented) and to send messages correctly.I'm using node xmpp server/examples/server-and-client.js.Thanks for advices.
2 Answers
This complete code work pretty well for me:
var xmpp = require('node-xmpp-server');
function generateRoster(jid, name, id, to) {
// This is a roster request, we create the response node
var roster = new xmpp.Element('iq', {
id: id,
to: to,
type: 'set'
});
roster.c('query', {xmlns: 'jabber:iq:roster', ver: 'ver13'})// We add a children tag `query`, with the two attribute xmlns and ver
.c('item', { // We add a children 'Item'
jid: jid, // We send the jid and the name
name: name,
subscription: 'both'
}).c('group').t('Connected Clients'); // We add it to the 'Connected Clients' group
return roster;
}
var startServer = function (done) {
// Sets up the server.
server = new xmpp.C2S.TCPServer({
port: 5222,
domain: 'localhost'
});
var connectedUsers = [];
var clientsHandles = {};
// On connection event. When a client connects.
server.on('connection', function (client) {
var userJid = null;
// That's the way you add mods to a given server.
// Allows the developer to register the jid against anything they want
client.on('register', function (opts, cb) {
console.log('REGISTER');
console.log(cb);
cb(false)
});
// Allows the developer to authenticate users against anything they want.
client.on('authenticate', function (opts, cb) {
//console.log('server:', opts.username, opts.password, 'AUTHENTICATING')
if (opts.password === 'secret') {
//console.log('server:', opts.username, 'AUTH OK')
cb(null, opts)
}
else {
//console.log('server:', opts.username, 'AUTH FAIL')
cb(false)
}
});
client.on('online', function () {
userJid = client.jid.user + '@' + client.jid.domain + '/' + client.jid.resource;
console.log(userJid + 'ONLINE');
for (var i = 0; i < connectedUsers.length; i++) {
var myRoster = generateRoster(userJid, userJid, 'myRandomId', connectedUsers[i]);
if (clientsHandles[connectedUsers[i]]) {
clientsHandles[connectedUsers[i]].send(myRoster);
console.log("Sending my new infos to ", connectedUsers[i]);
}
}
connectedUsers.push(userJid);
client.jid.userJid = userJid;
clientsHandles[userJid] = client;
});
// Stanza handling
client.on('stanza', function (stanza) {
if (stanza.is('message') && (stanza.attrs.type !== 'error')) {
if (clientsHandles[stanza.attrs.to]) {
clientsHandles[stanza.attrs.to].send(stanza);
}
}
else if (stanza.is('presence')) {
// We loop through the user list
for (var j = 0; j < connectedUsers.length; j++) {
console.log(stanza.toString());
var jid = connectedUsers[j];
stanza.to = jid;
clientsHandles[jid].send(stanza);
}
}
else if (stanza.is('iq') && stanza.attrs.type == 'get') {
for (var i = 0; i < stanza.children.length; i++) {
if (stanza.children[i].name == 'query' && stanza.children[i].attrs.xmlns == 'jabber:iq:roster') {
// We loop through the user list
for (var j = 0; j < connectedUsers.length; j++) {
var roster = generateRoster(connectedUsers[j], connectedUsers[j], stanza.attrs.id, stanza.attrs.from);
client.send(roster); // We send it back to the client
}
}
}
client.send(stanza);
}
else {
client.send(stanza);
}
});
// On Disconnect event. When a client disconnects
client.on('disconnect', function () {
if (userJid) {
console.log(userJid, "DISCONNECTED");
connectedUsers.splice(connectedUsers.indexOf(userJid), 1);
delete clientsHandles[userJid];
}
});
});
server.on('listening', done)
};
startServer(function () {
console.log("Server running");
});
When a message stanza arrives, I check wether the receiver is connected or not and if so I send the stanza to it.

- 324
- 2
- 7
-
Ok..i wil try and i will come back :) – Worker1 Aug 25 '15 at 11:28
-
I'm connected with two instances of Spark..I'm sending a message from user1 to user2 ..user2 don't receive the message – Worker1 Aug 25 '15 at 11:35
-
One more thing is here..Users seems to be offline but they are online...What should i do in this situation ? – Worker1 Aug 25 '15 at 11:43
-
what i should do ?I see you know very well how to work with xmpp – Worker1 Aug 25 '15 at 11:55
-
This works in almost all of the cases, but it should be corrected. To support the online feature you just have to broadcast the presence messages when you receive them. – Alekos Filini Aug 25 '15 at 18:08
-
First i want to thank you for your help..was great..i ask you one more question...My objective is to make a chat application.As a client what should i use(i need something for Angular JS) ? – Worker1 Aug 26 '15 at 06:58
-
You can use the node-xmpp-client library, which should work on the browser too – Alekos Filini Aug 26 '15 at 07:11
-
Yeah, no problem. Maybe it could be better to open a new question, then link it here and tag me – Alekos Filini Aug 26 '15 at 07:20
-
OK! I will do this :) – Worker1 Aug 26 '15 at 07:21
-
you can connect from node-xmpp-client to server that you post here ? – Worker1 Aug 26 '15 at 08:11
-
Here is the link of question @Alekos Filini : http://stackoverflow.com/q/32221452/5247401 – Worker1 Aug 26 '15 at 08:23
Send the message with the correct client connection i.e.
1). Store all the connection of the clients in an array at the time of authentication.
2). When a message come find the correct client associated with the "to" field in the message stanza.
var receiverCleint = getClient(msgStanza.attrs.to)
3). return the correct client and send the message through that client.
receiverCleint.send(msgStanza)
I am also working on the same demo, my only concern is when more user adds then it will be a problem as it has to iterate on each message through all clients.
Any better approach appreciated.
Code:
var clients = new Array();
c2sRouter.on("connection", function(client){
client.on("register", function(opts, cb){
console.log("client server", "register");
})
client.on("authenticate", function(opts, cb){
console.log("client server", "authenticate");
if(opts.password === "tushar" || opts.password === "tushar1"){
clients.push(client);
cb(null, opts);
}else{
cb(false, opts);
}
})
client.on("online", function(){
console.log("client server", "online");
})
client.on("stanza", function(stanza){
console.log("client server", stanza.toString());
if(stanza.is("message")){
var receiverClient = getClient(stanza.attrs.to)
if(receiverClient === undefined){
client.send(stanza);
} else {
receiverClient.send(stanza);
}
}
})
client.on("disconnect", function(){
console.log("client server", "disconnect");
})
})
var getClient = function (to) {
var clientLength = clients.length;
var client
var clientId;
for(var i = 0; i < clientLength; i++){
client = clients[i];
clientId = client.jid.user + "@" + client.jid.domain
if(to.indexOf(clientId) == 0){
return client;
}
}
}

- 719
- 13
- 23

- 97
- 2
- 7
-
-
Do you used this code with Spark client?Try to connect to server with two instances of Spark..check if it's work..To communicate in this instances – Worker1 Aug 25 '15 at 08:30