| How to develop whatsapp type chat engine using node |

We just developed a Chat Server using node. The node is a nonblocking environment we prefer to build a chat server for one of our client using Node.

Node has very good support for TCP Sockets and we used them to connect with clients. We again differ here with most of the open source ready made the server as we select the communication protocol as JSON, most of another open source available are on XML.JSON processing in Node is simpler and much faster.

Another reason was Node has nonblocking architecture hence we need not wait for the process to get completed the way we would be doing in C/C++ or any other platform.

How it works

For user management we use MongoDB, we keep all data in MongoDB so for each chat to initiate client must authenticate itself.We check all authentication parameter in MongoDB and if it matches we keep the object structure of the client in RAM.

Being nonblocking technology we need not create multiple process/threads for each communication channel which simplifies the whole process and reduce the amount of CPU required to run chat server.

Whenever there is a new connection incoming we create a session-id and save it in DB, and when user disconnects or some error/exception happen on the same session-id we deleted it from DB so that we keep our Session alive in DB.

For each new message, there will be an event called which pass all information to the caller like the message, socket id which can be used to process the message.

What Happens when receiver is not online

We can detect if the receiver is not online by seeing session-id, not in DB. We handle this message by sending the message to mobile using notifications.

We started using Google Cloud Messaging but many times it failed to deliver messages or messages are not coming in real time, we then used MQTT to send messages.

In this way we make sure messages are sent to mobile phones real time, non-lossy way. MQTT can be integrated with MongoDB to store messages and keeps track on which user has not received the messages and deliver the messages once the user gets online.