Home » Nodejs » How do I integrate chat with nodejs and xmpp into my existing web application?

How do I integrate chat with nodejs and xmpp into my existing web application?

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have read a lot of questions relating this, but none of them are satisfying.

Existing App

A minimalist social network implemented using Expressjs as an
API.Using MySql as DB.socket.io for notifications and
ember.js as a frontend framework.

What I want to integrate

I want to implements only a few features of XMPP such as

  • Peer 2 Peer Messaging
  • Presence and Last Seen
  • Group Chat
  • Read Receipts

A basic idea I got from reading similar questions.

  1. Need a client library (Strophe.js,Converse.js)
  2. Need a XMPP server (ejabberd,Openfire,Prosody)

Questions

  1. How do I integrate chat here ?
  2. How do I authenticate XMPP users (FYI, I have JWT Authentication implemented currently) ?
  3. Suggestions on using redis(pub/sub) with socket.io or mqtt pub/sub for implementing the chat.Is it scalable ? / What about performance ?

What I asked might be too broad.But still don’t have any idea on using which set technologies to use.

Answers:

I suggest to use socket.io, since you are currently using JWT authentication and it can be implementing authentication using NodeJS. You can create so call room in socket.io for peer to peer to messaging or goroup chat. I read your comment and since it’s for learning purposes Mysql is scalable enough. For the sake of performance using a load balancer like nginx or even can use NodeJS load balancer with horizontally scaling technique can extend performance easily. Hope it help.

Questions:
Answers:

For learning purpose you can achieve all things using ejabberd+converse.js
Below steps will setup environment in ubuntu

  1. setup ejabberd by following https://www.digitalocean.com/community/tutorials/how-to-install-ejabberd-xmpp-server-on-ubuntu
  2. create a host binding by editing /etc/hosts file in ubuntu

    127.0.1.2       talk.rajesh6115.local
    
  3. install apache2 using

    sudo apt-get update
    sudo apt-get install apache2
    
  4. setup a virtual host for bosh (XEP-0206) in your apache like below
    /etc/apache2/sites-available/talk.rajesh6115.local.conf

    <VirtualHost *:80>
        ServerName talk.rajesh6115.local
        ServerAlias www.talk.rajesh6115.local
        ServerAdmin [email protected]
        DocumentRoot /var/www/talk.rajesh6115.local
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        ProxyPass /http-bind http://talk.rajesh6115.local:5280/http-bind/
        ProxyPassReverse /http-bind http://talk.rajesh6115.local:5280/http-bind/
    </VirtualHost>
    
  5. now you can configure converse js to point to your bosh service, then your communications starts

NOTE:

setup a virtual host in ejabberd by adding a line like below

    hosts:
      - "talk.rajesh6115.local"

setup a admin login. using this login you can create user.

  admin:
     user:
         - "admin": "talk.rajesh6115.local"
  1. for make conversejs talk with xmpp server only one thing you have to give that is bosh serivce url. for more details
    https://conversejs.org/docs/html/development.html#initialize

7.finally how to integrate with web application?

Method1 (simple):
use same logins for webapp and xmpp means [email protected] can be a email address also a valid jid

Method2:
Use a authentication server which will return both jid and password after successful authentication, then start your xmpp session using provided credential.

Questions:
Answers:

Given your original idea how to solve this, I’d suggest that you take a look at node-xmpp-client and node-xmpp-server.

It’s an excellent set of libraries and you can use them to fully integrate your application on a nodejs level. So you’d be able to control authentication yourself (use existing users/pws in your app?), and be notified when a message in a (group) chat appears.

Of course you can use an existing server like prosody or ejabberd, as a backend for chats. In my experience it’s not much work to get node-xmpp-client integrated. But building/running an XMPP-server with nodejs (that can actually talk to other servers) is not that trivial – a little more that the examples thrown together yield, unfortunately.

Also, XMPP being text-based, actually even worse, xml-based… it’s not really the definition of efficient. Let alone the complexity of all the modules supporting node-xmpp 🙂

So

If you are worried about performance and don’t need XMPP per-se and really only want the features above, XMPP is a bad choice. It’s far to wasteful for your original purposes.

So something like zmq should enable you to implement group and personal chats.

redis could be used to save chat histories, presence information and message reciepts.

To my knowledge there is no library for node that would just give you what you want for free, and IMHO the way using XMPP is even harder than implementing your features with tools like zmq and a datastore as backing on your own.