Welcome folks today in this blog post we will be building a live comments section using pusher.js in node.js and express using websockets in browser. All the full source code of the application is shown below.
Get Started
In order to get started you need to initialize a new node.js
project using the below command
npm init -y
npm i express
npm i pusher
As you can see we are installing the above packages which are express and pusher. And express is the http server & pusher is the library for realtime comment section using websockets.
Directory Structure of App
And now inside the server.js file we will copy paste the below code to start the express server as shown below
server.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var express = require('express'); var path = require('path'); var bodyParser = require('body-parser'); var app = express(); // Error Handler for 404 Pages app.use(function(req, res, next) { var error404 = new Error('Route Not Found'); error404.status = 404; next(error404); }); module.exports = app; app.listen(9000, function(){ console.log('Example app listening on port 9000!') }); |
As you can see we are starting the express app at the 9000 port number. And then we are also including the middleware of 404 error which returns a custom message to the user not found. When you visit a wrong route you will see this message.
Including the Middlewares
So now inside this code we will be including the middlewares such as bodyParser inside express app as shown below. And also we will be setting the public directory as an static directory so that we can store the client files such as html, css and javascript files. And also the asset files such as images.
1 2 3 4 5 |
const bodyParser = require('body-parser') app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(express.static(path.join(__dirname, 'public'))); |
Now after this you need to make a public directory inside the project folder as shown below
Initialize Pusher.js Library
Now we will be initialize a pusher.js library inside the express app. We will write the below code for it.
1 2 3 4 5 6 7 8 9 |
var Pusher = require('pusher'); var pusher = new Pusher({ appId: '', key: '', secret: '', cluster: 'ap2', encrypted: true }); |
As you can see in the above code we need to get these details from the pusher.js dashboard. After you signup on pusher.js for free account you will get appId , key and secret information as shown below
As shown above these details will be different for you. You just need to copy these details and then include the details in express app.
Making the Client HTML & JS Files
Now we will be making the client html and javascript files for the project. Now first of all inside the public folder you need to make the index.html
file and copy paste the below code
public/index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<!DOCTYPE> <html> <head> <title>Making Social Comments Realtime & Fun with Pusher using Javascript like the Flash</title> <link rel="stylesheet" href="https://unpkg.com/purecss@0.6.2/build/pure-min.css" crossorigin="anonymous"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway:200"> </head> <body> <section> <div class="flash-comments"> <div class="header"> <div class="text">Comments</div> </div> <form class="pure-form" id="comment-form"> <div class="comment-form"> <div class="left-side"> <div class="row"> <input type="text" required placeholder="enter your name" id="new_comment_name"> <input placeholder="enter valid email" required type="email" id="new_comment_email"> </div> <div class="row"> <textarea placeholder="enter comment text" required id="new_comment_text" rows="3"></textarea> </div> </div> <div class="right-side"> <button type="submit" class="button-secondary pure-button">Send Comment</button> </div> </div> </form> <div class="comments-list" id="comments-list"> <script id="comment-template" type="text/x-template"> <div class="user-icon"> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOUAAADcCAMAAAC4YpZBAAABd1BMVEX///+QlK8BRG/vppfkb2X///3ibGAAQGuvvMdMaY0ANmcAQm7//v+QlK2Pk6/9//8APWwAPW0AQnAAPWqanLPzqJj4rJeKjKeVmLIAPG6MlrOOkaoAO2mcnrUzSW+Sk6/qbWEAO3Doqp/ioJG7naQALWaTlarldWrvpptQWnbGyNTc3ea8wM6GjKVfdZMbTHA6XYLp6u6lqLnVm5OFdILkf3PvoZO8j4vpi3/oraAAMmaytMPS09vz8/fj5OslU3w/ZYN5f6I3WYJkeJB2hKBYdZVNZItsgaFfYXqphY/sp5C/k4tVY3+WfIozVnU/Unffnph0a4SkfYiMd4A9T2rQcHR5bXqPV2uBV2pMTGqYYGjyhnZvVmZmUXHSZ168aGqnXmOCUW7llIfSamaKf5Oyl6f/qJVJTXIPRWSZcHptY4XOoKHBmpgJV37BhoSiipHUf3jlY1vzblW2gpDr2Nfzu7L1x8H96+d3jqKQpLMAHVyVqbxgf5P0YuCQAAAYFklEQVR4nO1djV/aWNaG0Bgh3IQkJCQGG4Ea4wcEtaKiiNviWOzUatvtdGZ32n3ttr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JwBK6uPmD1a410beD3br/+Q4SWSYorKVu1wOYdF5W/RYOcFmyUFkv+JsfPzzA07WLhL8YAZclU/0S2sRdQPDQC+09rPrJq/FYSvMHaJX/1yDJNva+VMfLE1W3pg9qvlim5g9uOqAbCyTPL/O5MUEfZMknfr8cz5KV5h8u07c2egwDjXjWPoujDRRp7PRFbVw9VkxIF6todcXtxAGjAXny0/vzI85oTThhGhTRCJasNl+eTt5eFDAWztC9emlX0eJsrwQU2SWP7t+DJWpiQLHZqv15D1WXbimew8Dy5/q857nYI30sK2qQ4u7XT8+Bs31r9fJAqmr9y0iGsmQlLZGNX+6hvRhfrap6orb7+SAxn+jeWuDJUtTgq+Kfl26mABkyHA/J701f7NuJajWbTTEs28WSZZlUNpGoJuz9i+k9pKZ/MCG6QGWo1n3XlqcvL77E68zv0+4q4Ei5ytT3v1xcTi+3ziDlnUnqPzbaIx/f2jtLt+T2R+fVAxTEJKFbSfIt6bpyvqnq8QQTTDDBBBNMMMEEE0wwwe3i/wEJNTpxa4tIkQAAAABJRU5ErkJggg==" /> </div> <div class="comment-info"> <div class="row"> <div class="name">{{name}}</div> <div class="email">{{email}}</div> </div> <div class="row"> <div class="text">{{comment}}</div> </div> </div> </script> </div> </div> </section> <script type="text/javascript" src="https://js.pusher.com/3.2/pusher.min.js"></script> <script type="text/javascript" src="./app.js"></script> </body> </html> |
As you can see we have the html5 form where we have three fields and a button to build the live comment section where the user will be able to comment in the website. The fields are name,email address and the actual comment and then we have the submit button. After that we will handling the submission of the form using ajax in javascript. And lastly we are including the pusher.js cdn inside this file and also including the custom javascript app.js
file at the bottom.
And now we will be making the javascript file app.js
and then we will copy paste the below code
public/app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Using IIFE for Implementing Module Pattern to keep the Local Space for the JS Variables (function() { // Enable pusher logging - don't include this in production Pusher.logToConsole = true; var serverUrl = "/", comments = [], pusher = new Pusher('021642595f012acd8c78', { cluster: 'ap2', encrypted: true }), // Subscribing to the 'flash-comments' Channel channel = pusher.subscribe('flash-comments'), commentForm = document.getElementById('comment-form'), commentsList = document.getElementById('comments-list'), commentTemplate = document.getElementById('comment-template'); // Binding to Pusher Event on our 'flash-comments' Channel channel.bind('new_comment',newCommentReceived); // Adding to Comment Form Submit Event commentForm.addEventListener("submit", addNewComment); })(); |
And now inside the above code we are having the IIFE (immediately invoked function expression) at the top inside this block we are executing the javascript code. In this first of all we are initializing the pusher.js library and inside that we are passing the api key. And then we are passing the cluser and encrypted option. And then basically we are getting the references of all the DOM Elements. And then we are subscribing to the channel which is called flash-comments using the pusher.js subscribe method. And we storing that reference inside the channel variable. And then we are binding the a new event to the channel which we have subscribed. These events will be sent from the server to this channel and we are receiving this event using the bind method and this method will be executed which is newCommentReceived. And also we are attaching the addEventListener to the comment form which is called addNewComment Method.
And here also we have defined the serverUrl which is equal to /
and also we are declaring the empty comments array.
Sending New Comment to Server Using AJAX
Now we will be using some AJAX to send the newly written comment inside the html5 form. When we click the submit button we will execute this below function in which we will initialize a new XMLHttpRequest() and send it to server. This will be a POST Request and sending that data inside an object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function addNewComment(event){ event.preventDefault(); var newComment = { "name": document.getElementById('new_comment_name').value, "email": document.getElementById('new_comment_email').value, "comment": document.getElementById('new_comment_text').value } var xhr = new XMLHttpRequest(); xhr.open("POST", serverUrl+"comment", true); xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.onreadystatechange = function () { if (xhr.readyState != 4 || xhr.status != 200) return; // On Success of creating a new Comment console.log("Success: " + xhr.responseText); commentForm.reset(); }; xhr.send(JSON.stringify(newComment)); } |
As you can see we are receiving the event parameter inside the function. First of all using this event object we are preventing the auto submission of the form and then we are making the object containing three properties such as name,email and comment. And then we are making a AJAX request and then opening a new POST Request and then we are embedding the URL and last parameter to true. And then we are setting the request header which is content-type to application/json. And then if the ajax request is ready and the state changes of the request. Inside that we have the if block we are checking the status of the request which is equal to 200. In that case we are resetting the comment form fields. And also writing the response coming from the server inside the console.
And lastly we are sending the ajax request to the server using the send() method. And also we are converting the javascript object to the json object using the JSON.stringify() method.
Making the POST Request in the Server
Here we will be defining the POST Request inside the server file. The post request route will be /comment. The code is shown below
1 2 3 4 5 6 7 8 9 10 |
app.post('/comment', function(req, res){ console.log(req.body); var newComment = { name: req.body.name, email: req.body.email, comment: req.body.comment } pusher.trigger('flash-comments', 'new_comment', newComment); res.json({ created: true }); }); |
And also inside this post request we are making a new Javascript object containing all the three properties such as name email and comment. These values we are getting using the bodyparser middleware and we are getting the values from the body header using request class. And then we are passing the event back to the client using pusher.js. Here we are using the trigger() method to send the event which is called flash-comments and then we are passing the name of the event which is new-comment and the actual comment which was created is returned is newComment which is basically an object. And then we are returning the json object and sending the response to the client.
Showing Live Comments in HTML
Now lastly we will be showing the comments coming from the server in the form of an object. We need to show them inside the DOM using some dynamic elements.
1 2 3 4 5 6 7 8 9 |
function newCommentReceived(data){ var newCommentHtml = commentTemplate.innerHTML.replace('{{name}}',data.name); newCommentHtml = newCommentHtml.replace('{{email}}',data.email); newCommentHtml = newCommentHtml.replace('{{comment}}',data.comment); var newCommentNode = document.createElement('div'); newCommentNode.classList.add('comment'); newCommentNode.innerHTML = newCommentHtml; commentsList.appendChild(newCommentNode); } |
As you can see we are receiving the data inside the above function. And inside this function we are replacing the dynamic variables which are actual name,email address and the actual comment inside the template of html5. For this we are using the replace() method and then we are creating the dynamic element which is div and then we are manipulating the innerHTML of the comment and then lastly we are appending the comment to the DOM.
Full Source Code
Now we will be providing you the full source code of all the files. First of all we have the server.js
file
server.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
var express = require('express'); var path = require('path'); var bodyParser = require('body-parser'); var Pusher = require('pusher'); var pusher = new Pusher({ appId: '1492649', key: '021642595f012acd8c78', secret: 'be596a61540a3badb86a', cluster: 'ap2', encrypted: true }); var app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(express.static(path.join(__dirname, 'public'))); app.post('/comment', function(req, res){ console.log(req.body); var newComment = { name: req.body.name, email: req.body.email, comment: req.body.comment } pusher.trigger('flash-comments', 'new_comment', newComment); res.json({ created: true }); }); // Error Handler for 404 Pages app.use(function(req, res, next) { var error404 = new Error('Route Not Found'); error404.status = 404; next(error404); }); module.exports = app; app.listen(9000, function(){ console.log('Example app listening on port 9000!') }); |
Now we will be sharing the index.html
file
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<!DOCTYPE> <html> <head> <title>Making Social Comments Realtime & Fun with Pusher using Javascript like the Flash</title> <link rel="stylesheet" href="https://unpkg.com/purecss@0.6.2/build/pure-min.css" crossorigin="anonymous"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway:200"> </head> <body> <section> <div class="flash-comments"> <div class="header"> <div class="text">Comments</div> </div> <form class="pure-form" id="comment-form"> <div class="comment-form"> <div class="left-side"> <div class="row"> <input type="text" required placeholder="enter your name" id="new_comment_name"> <input placeholder="enter valid email" required type="email" id="new_comment_email"> </div> <div class="row"> <textarea placeholder="enter comment text" required id="new_comment_text" rows="3"></textarea> </div> </div> <div class="right-side"> <button type="submit" class="button-secondary pure-button">Send Comment</button> </div> </div> </form> <div class="comments-list" id="comments-list"> <script id="comment-template" type="text/x-template"> <div class="user-icon"> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOUAAADcCAMAAAC4YpZBAAABd1BMVEX///+QlK8BRG/vppfkb2X///3ibGAAQGuvvMdMaY0ANmcAQm7//v+QlK2Pk6/9//8APWwAPW0AQnAAPWqanLPzqJj4rJeKjKeVmLIAPG6MlrOOkaoAO2mcnrUzSW+Sk6/qbWEAO3Doq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VZe0xl0Xcfslfzllh4spd8vyeySynYVobCW4eMgSbdXdAEYqpPKkl6+TBLKMlUCHUnOXtvZ0XQk2U5P8EL1bpbJCOlIQon7bZUFZjLMcKCXJU0nW33CixIpywg5S8pu76EHNfpaz66vOfOKgPC0uWAsWfeAFhA1a9d6ojsc6pA0gbpPdl5OQFm6h+0A8xqigX6iSbQzj/CsuYAsmW/UqHOa+01gjSMK1UNgWVej3Oz1S9JR2shO6XZYUikzs5a8XsfThcv5W7FLSjoMeHgODnh6uup5Rs41s2Tnp2+OJMJuKnXjLJkq6SkApKBr8TGTjqGzTNRXry3gGc7zYh47kQ7Ccv7iJnW1jSS9y+DeKzlLITuNsbkpPMAnWzvwPwUQiCVb3V+NXFsWMhrwstM5rEM9CVmmEjsRjJap4YLm+UjtC7RO3/ZJwBK6uPmD1a410beD3br/+Q4SWSYorKVu1wOYdF5W/RYOcFmyUFkv+JsfPzzA07WLhL8YAZclU/0S2sRdQPDQC+09rPrJq/FYSvMHaJX/1yDJNva+VMfLE1W3pg9qvlim5g9uOqAbCyTPL/O5MUEfZMknfr8cz5KV5h8u07c2egwDjXjWPoujDRRp7PRFbVw9VkxIF6todcXtxAGjAXny0/vzI85oTThhGhTRCJasNl+eTt5eFDAWztC9emlX0eJsrwQU2SWP7t+DJWpiQLHZqv15D1WXbimew8Dy5/q857nYI30sK2qQ4u7XT8+Bs31r9fJAqmr9y0iGsmQlLZGNX+6hvRhfrap6orb7+SAxn+jeWuDJUtTgq+Kfl26mABkyHA/J701f7NuJajWbTTEs28WSZZlUNpGoJuz9i+k9pKZ/MCG6QGWo1n3XlqcvL77E68zv0+4q4Ei5ytT3v1xcTi+3ziDlnUnqPzbaIx/f2jtLt+T2R+fVAxTEJKFbSfIt6bpyvqnq8QQTTDDBBBNMMMEEE0wwwe3i/wEJNTpxa4tIkQAAAABJRU5ErkJggg==" /> </div> <div class="comment-info"> <div class="row"> <div class="name">{{name}}</div> <div class="email">{{email}}</div> </div> <div class="row"> <div class="text">{{comment}}</div> </div> </div> </script> </div> </div> </section> <script type="text/javascript" src="https://js.pusher.com/3.2/pusher.min.js"></script> <script type="text/javascript" src="./app.js"></script> </body> </html> |
app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
// Using IIFE for Implementing Module Pattern to keep the Local Space for the JS Variables (function() { // Enable pusher logging - don't include this in production Pusher.logToConsole = true; var serverUrl = "/", comments = [], pusher = new Pusher('021642595f012acd8c78', { cluster: 'ap2', encrypted: true }), // Subscribing to the 'flash-comments' Channel channel = pusher.subscribe('flash-comments'), commentForm = document.getElementById('comment-form'), commentsList = document.getElementById('comments-list'), commentTemplate = document.getElementById('comment-template'); // Binding to Pusher Event on our 'flash-comments' Channel channel.bind('new_comment',newCommentReceived); // Adding to Comment Form Submit Event commentForm.addEventListener("submit", addNewComment); // New Comment Receive Event Handler // We will take the Comment Template, replace placeholders & append to commentsList function newCommentReceived(data){ var newCommentHtml = commentTemplate.innerHTML.replace('{{name}}',data.name); newCommentHtml = newCommentHtml.replace('{{email}}',data.email); newCommentHtml = newCommentHtml.replace('{{comment}}',data.comment); var newCommentNode = document.createElement('div'); newCommentNode.classList.add('comment'); newCommentNode.innerHTML = newCommentHtml; commentsList.appendChild(newCommentNode); } function addNewComment(event){ event.preventDefault(); var newComment = { "name": document.getElementById('new_comment_name').value, "email": document.getElementById('new_comment_email').value, "comment": document.getElementById('new_comment_text').value } var xhr = new XMLHttpRequest(); xhr.open("POST", serverUrl+"comment", true); xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xhr.onreadystatechange = function () { if (xhr.readyState != 4 || xhr.status != 200) return; // On Success of creating a new Comment console.log("Success: " + xhr.responseText); commentForm.reset(); }; xhr.send(JSON.stringify(newComment)); } })(); |