Real time commenting System with Node and MongoDB | Real time commenting on post using Node.js and

Nodejs comment system example

Nodejs comment system example

So many applications required real time programming and believe me Node.js and is the best combination or that. So many applications required real time commenting system. Today in this article you are going to learn how you can comment in real time in Node.Js by using

In my previous Node.js tutorial I explained video streaming in Node.js with the help of
Today also I'm going to use for creating the real time comment system in node.js.

I'll create a simple commenting system. There will be a listing of posts. I'll be fetching the posts from MongoDB. You also will be able to view detail of every post. On detail page of posts you could comment in real time. I'm also saving your comments in MongoDB along with real time commenting. So whenever you'll see the post detail you could see the comments.

To start with let's define package.json which contains your dependencies.

I have used followings in package.json:

  • EJS templating for view.
  • Express for routing.
  • Mongoose for querying with MongoDB.
  • for showing comments in real time.


To install all the dependencies run the below command, It'll download all the node modules by NPM (Node package manager).

Because we are storing the posts and comments in MongoDB so we require to create the schema for our application.

Start your Mongo server , Run below command to start the Mongo server:

I have used database named as 'posts_db'. You can create Mongo database by using following command:

Now we'll require to create the collections. I have used 'posts' collection to store the posts and 'comments' collection for storing the comments. To create posts and comments collection now to store the posts and comments:

Because we are not creating posts here so I'll be inserting some dummy data in 'posts' collection for displaying the posts so that you can comment on posts. Run below Mongo command to insert the posts data:

Now you are done with database. You need to create the schema in Node.Js now. In schema we'll just defining our collection fields.
Create a new directory named as 'schema'. Here in this directory I'll define all the schema.


In posts schema I have defined my fields of posts like title, description, by(post created by) and URL.


In comments schema I have defined my fields of comments like comment and postId(unique id of post).

Now create a new JavaScript file like I have created app.js. I have included all the modules which are required in my App. I have used for real time commenting. I also have created database connection with MongoDB here in app.js.

Whenever a socket connection is created and user send comment I'll save the comment into the database and broadcast the data.


Here is view page which is home page of my App. In this page I'm just displaying all the posts and against each post there is link to view the detail of post.


Here is the post detail page. I'm displaying the post detail and below that I'm listing all the comments which are already commented on the individual post.

Below that I have a comment form where you can comment on the post. Whenever you will post a comment, I'll append the comment to the post and will emmit the data.

Later I have a callback function when the comment is broad casted I'll check the post id and will append the comment if it matched with the post detail id.

Create a a new directo

After all setup run you project with following command:

Download code for real time commenting system in Node.js

Share This:

2 thoughts on “Real time commenting System with Node and MongoDB | Real time commenting on post using Node.js and

Leave a Reply

Your email address will not be published. Required fields are marked *