Xmpp Chat Using Node Js
Node js is a software platform, which is used to build server side applications, that means it’s a server side javascript, which uses javascript as its scripting language. Node js achieves high throughput via event driven programming techniques and non-blocking I/O.
What is xmpp chat
Extensible Messaging and Presence Protocol (XMPP), is a middleware based on XML which is used for messaging operations. Pidgin is the best example for xmpp chat.
Xmpp chat
For sorting out compatible plugins for our nodejs application, we need to perform a search using https://www.npmjs.org/, while doing so, we will come acroos a module node-xmpp , just include it in your application for xmpp chat.
var xmpp = require('node-xmpp'); In order to achieve a chat system using xmpp, first : we need an xmpp chat server second : establish connection to the chat server var connection = new xmpp.Client({ jid: 'admin@'+domain, password: '123456', }); connection.on('online', function(data) { });
the above code helps to achieve connection to the chat server
There are two functions which helps us to send and receive informatiinformationons between two clients
a)connection .send
b)connection .on
a)connection .send
this function is used to send our presence, chats etc to the chat server, after establishing the connection to the chat server we need to send our presence to the server
third : connection.send(new xmpp.Element(‘presence’, {‘type’:’available’})
.c(‘show’).t(‘chat’).up()
.c(‘status’).t(‘Hi I am here <message/> stanzas’));
We referred each info send using send methods as stanza, when we send this presence stanza chat server considered this user as online.
How to write different stanzas
Actually we can send stanzas in raw xml format, sometimes it makes problems, so it is good to convert it to our npm module’s format [node-xmpp]
How to convert xml element to node-xmpp element format
<presence > <show>dnd</show> <status>Wooing Juliet</status> <status xml:lang='cz'>Ja dvořím Juliet</status> </presence> new xmpp.Element('presence', {'type':'available'}) .c('show').t('dnd').up() .c('status').t('Ja dvořím Juliet')
just send this element to make you available
How to send single Chat
var stanza = new xmpp.Element('message', {to: receiver's jid, type: 'chat',id:uniqueid}). c('body').t(msg)
connection.send(stanza.tree());
How to send group Chat
var to = university+'@conference.'+domain; var stanza = new xmpp.Element('message', {to: to, type: 'groupchat',id:unique id}). c('body').t(msg); connection.send(stanza.tree()); How to add rosters var iq = new xmpp.Element( 'iq', { from:user,type: 'set'}) .c('query', {'xmlns': 'jabber:iq:roster'}) .c('item',{'jid':jid,'name':name,'subscription':'none'}); connection.send(iq);
Like this all xml elements can be converted to this format, and we can send
b)connection .on
This function acts as a receiver,
connection.on('online', function(data) { connection.on('stanza', function(stanza) { }); });
Once we establish a connection and send an online presence callback function receives, chat and other info send to our jid, until we closed the connection.
How to close connection
connection.end()
Node js is a fast-growing web application development technique, xmpp in conjunction with node.js will help you to develop a versatile and reliable chat system. The chat system thus developed can be customized with respect to your requirements. Hence, you need not rely on other costly chat applications.
Courtesy: http://nodejs.org/