Cómo construir una aplicación de chat en tiempo real en Node.js usando Express, Mongoose y Socket.io

En este tutorial, utilizaremos la plataforma Node.js para crear una aplicación de chat en tiempo real que envía y muestra mensajes a un destinatario al instante sin actualizar la página. Usaremos el framework de JavaScript Express.js y las bibliotecas Mongoose y Socket.io para lograr esto.

Antes de comenzar, echemos un vistazo rápido a los conceptos básicos de Node.js

Node.js

Node.js es un entorno de tiempo de ejecución de JavaScript multiplataforma de código abierto que ejecuta código JavaScript fuera del navegador. La ventaja más importante de usar Node es que podemos usar JavaScript como lenguaje tanto de front-end como de back-end.

Como sabemos, JavaScript se utilizó principalmente para scripts del lado del cliente, en los que los scripts se incrustaban en el HTML de una página web y se ejecutaban en el lado del cliente mediante un motor de JavaScript en el navegador web del usuario.

Node.js permite a los desarrolladores usar JavaScript para escribir herramientas de línea de comandos y para scripts del lado del servidor: ejecutar scripts en el lado del servidor para producir contenido dinámico de la página web antes de que la página se envíe al navegador web del usuario.

Para instalar el nodo:

//nodejs.org/en/download/

Aunque el nodo es de un solo subproceso, es más rápido usar funciones asincrónicas. Por ejemplo, Node puede procesar otras cosas mientras se lee un archivo del disco o mientras se espera que se complete una solicitud HTTP. El comportamiento asincrónico se puede implementar mediante devoluciones de llamada. Además, JavaScript funciona bien con bases de datos JSON y No-SQL.

Módulos NPM

Nodejs permite incluir módulos de bibliotecas en la aplicación. Estos módulos pueden ser módulos definidos por el usuario o de terceros.

Los módulos de terceros se pueden instalar usando el siguiente comando:

npm install module_name

y los módulos instalados se pueden usar usando la función require () :

var module = require(‘module_name’)

En las aplicaciones de nodo, usaremos un archivo package.json para mantener las versiones del módulo. Este archivo se puede crear con este comando:

npm init

y los paquetes deben instalarse de la siguiente manera:

npm install -s module_name

Hay muchos marcos que se pueden agregar como módulos a nuestra aplicación Node. Estos se explicarán más adelante según sea necesario.

Aplicación de chat simple

La aplicación debe permitir que varios usuarios conversen juntos. Los mensajes deben actualizarse sin actualizar la página. Por simplicidad evitaremos la parte de autenticación.

Podemos comenzar creando un nuevo directorio de proyecto y movernos a él. Entonces podemos iniciar nuestro proyecto con el siguiente comando:

npm init

Esto nos pedirá que ingresemos detalles sobre nuestro proyecto.

Después de esto , se creará un archivo package.json :

{ “name”: “test”, “version”: “1.0.0”, “description”: “”, “main”: “index.js”, “scripts”: { “test”: “echo \”Error: no test specified\” && exit 1" }, “author”: “”, “license”: “ISC” }

Nuestro directorio de aplicaciones ya está configurado.

Lo primero que necesitamos crear es un servidor. Para crear eso, usaremos un marco llamado Express.

Express.js

Express.js, o simplemente Express, es un marco de aplicación web para Node.js. Express proporciona un sólido conjunto de funciones para aplicaciones web y móviles. Express proporciona una capa delgada de funciones fundamentales de aplicaciones web, sin ocultar las funciones de Node.js.

Instalaremos Express.js usando el siguiente comando:

npm install -s express

Dentro del archivo package.json se agregará una nueva línea:

dependencies”: { “express”: “⁴.16.3” }

Next we will create a server.js file.

In this file we need to require Express and create a reference to a variable from an instance of Express. Static contents like HTML, CSS or JavaScript can be served using express.js:

var express = require(‘express’); var app = express();

and we can start listening to a port using the code:

var server = app.listen(3000, () => { console.log(‘server is running on port’, server.address().port); });

Now we need to create an HTML file index.html that displays our UI. I have added bootstrap and JQuery cdn.

//index.html    
    

Send Message

Send

Please note that the empty <;/script>tag will be the place where we will write the client side JavaScript code.

In-order to tell Express that, we will be using a static file. We will add a new line inside server.js:

app.use(express.static(__dirname));

We can run the server.js using the command

node ./server.js

or a package called nodemon, so that the changes made in the code will be automatically detected. We will download nodemon using the command

npm install -g nodemon

-g — global, so that it is accessible in all projects.

We will run the code using the command

nodemon ./server.js

If you go to localhost:3000 we can see the index file:

Now that our server is up and running, we need to create our database. For this app we will having a No-SQL database and will be using Mongodb. I am setting up my mongodb in mlab.com. Our database will contain a single collection called messages with fields name and message.

In-order to connect this database to the app, we will use another package called Mongoose.

Mongoose

Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. Mongoose can be installed using the command

npm install -s mongoose

Inside server.js we will require mongoose:

var mongoose = require(‘mongoose’);

And we will assign a variable, the URL of our mlab database:

var dbUrl = ‘mongodb://username:[email protected]:57981/simple-chat’

Mongoose will connect to the mlab database with the connect method:

mongoose.connect(dbUrl , (err) => { console.log(‘mongodb connected’,err); })

And we will be defining our message model as

var Message = mongoose.model(‘Message’,{ name : String, message : String})

We can implement the chat logic now. But before that there is one more package that needs to be added.

Body-Parser

Body-Parser extracts the entire body portion of an incoming request stream and exposes it on req.body. The middleware was a part of Express.js earlier, but now you have to install it separately.

Install it using the following command:

npm install -s body-parser

Add the following codes to server.js:

var bodyParser = require(‘body-parser’) app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: false}))

Routing

Routing refers to how an application’s endpoints (URIs) respond to client requests. You define routing using methods of the Express app object that correspond to HTTP methods: app.get() to handle GET requests and app.post() to handle POST requests.

These routing methods specify a callback function (sometimes called “handler functions”) called when the application receives a request to the specified route (endpoint) and HTTP method. In other words, the application “listens” for requests that match the specified routes and methods, and when it detects a match, it calls the specified callback function.

Now we need to create two routes to the messages for our chat to work.

Inside server.js:

get : will get all the message from database

app.get('/messages', (req, res) => { Message.find({},(err, messages)=> { res.send(messages); }) })

post : will post new messages created by the user to the database

app.post('/messages', (req, res) => { var message = new Message(req.body); message.save((err) =>{ if(err) sendStatus(500); res.sendStatus(200); }) })

In order to connect these routes to the front end we need to add the following code in the client side script tag in the index.html:

$(() => { $("#send").click(()=>{ sendMessage({ name: $("#name").val(), message:$("#message").val()}); }) getMessages() }) function addMessages(message){ $(“#messages”).append(` 

${message.name}

${message.message}

`) } function getMessages(){ $.get(‘//localhost:3000/messages', (data) => { data.forEach(addMessages); }) } function sendMessage(message){ $.post(‘//localhost:3000/messages', message) }

Here the sendMessage is used to invoke the post route of the messages, and save a message sent by the user. The message is created when a user clicks the send button.

Similarly the getMessage is used to invoke the get route of messages. This will get all the messages saved in the database and will be appended to the messages div.

The only issue now is that there is no way for the client to know if the server is updated. So each time we post a message we need to refresh the page to see the new messages.

To solve this we can add a push notification system that will send messages from server to client. In Node.js we use socket.io.

Socket.io

Socket.IO is a JavaScript library for realtime web applications. It enables realtime, bi-directional communication between web clients and server. It has two parts: a client-side library that runs in the browser, and a server-side library for Node.js. Socket.io enables real-time bidirectional event-based communication.

To install socket.io:

npm install -s socket.io

we also need an HTTP package for Socket.io to work:

npm install -s http

Add the following code to server.js:

var http = require(‘http’).Server(app); var io = require(‘socket.io’)(http);

And we can create a connection:

io.on(‘connection’, () =>{ console.log(‘a user is connected’) })

In the index.html add the following tag:

Now we need to create an emit action when a message is created in server.js. So the post route becomes this:

app.post('/messages', (req, res) => { var message = new Message(req.body); message.save((err) =>{ if(err) sendStatus(500); io.emit('message', req.body); res.sendStatus(200); }) })

And in the client side script tag in index.html, add the following code:

var socket = io(); socket.on(‘message’, addMessages)

So each time a message is posted, the server will update the messages in the message div.

Great!!

This is very basic application that we can create in Node.js. There is lot of scope for improvement. The finished code can be found in //github.com/amkurian/simple-chat

server.js

var express = require('express'); var bodyParser = require('body-parser') var app = express(); var http = require('http').Server(app); var io = require('socket.io')(http); var mongoose = require('mongoose'); app.use(express.static(__dirname)); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: false})) var Message = mongoose.model('Message',{ name : String, message : String }) var dbUrl = 'mongodb://username:[email protected]:57981/simple-chat' app.get('/messages', (req, res) => { Message.find({},(err, messages)=> { res.send(messages); }) }) app.get('/messages', (req, res) => { Message.find({},(err, messages)=> { res.send(messages); }) }) app.post('/messages', (req, res) => { var message = new Message(req.body); message.save((err) =>{ if(err) sendStatus(500); io.emit('message', req.body); res.sendStatus(200); }) }) io.on('connection', () =>{ console.log('a user is connected') }) mongoose.connect(dbUrl ,{useMongoClient : true} ,(err) => { console.log('mongodb connected',err); }) var server = http.listen(3001, () => { console.log('server is running on port', server.address().port); });

Hope this was helpful in understanding some basic concepts.

Some useful links

Socket.IO

SOCKET.IO 2.0 IS HERE FEATURING THE FASTEST AND MOST RELIABLE REAL-TIME ENGINE ~/Projects/tweets/index.js var io =…socket.ioExpress - Node.js web application framework

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and…expressjs.com

//mongoosejs.com/