Suggestions needed regarding comment system (developers only!)

(Not a java developer) #1

some days back I posted about migrating from WordPress to nodejs. Since then i started building a CMS. the cms is partially complete but I am not able to add reply to comment feature to it. as i am not able to think how to implement it.Please suggest some concepts on how to implement this? or if you know how wordpress comment system works please tell.

(DND) #2

Please use Facebook or Discuss comment system.

Because … this will get very hectic in future … when comes to comment moderation and notification systems.

(Abhishek Pathak) #3

How much deep are you expecting your reply comments to be in the thread?

I am hoping a one level reply system won’t be an issue at all.

For multi-level reply system, you will need to store the
post_id of the post along with date created, by whom and other meta
comment_id of the one level-up comment, (different from _id of the comment)
comment_level for the level of comment (or weight)

With the model in place, query the database like,
-> get all the comments for that post_id sorted by date (and not hidden or deleted)

At the front-end, you’ll need to play with the comment_level.

First list all the comments with level 1, and under those, place the level 2 and so on corresponding to their main comment. I’m not sure if this is going to be an easy task at the front-end, cause I have not done this, just thought this approach while writing.

This is the WordPress way of display comments.
Cons of this approach - pagination in the query would be destroyed. :stuck_out_tongue:

Another and better approach could be simply calling the first level comments first and then hit another API for below level comments. This way, server bandwidth will be saved and response times will be faster.

Also let me know if you found any better solution. Thanks. :slight_smile:


Depending upon the technology you are using to design your site, you might find some premade packages and plugins for it, as this features are so essential and common.

(Not a java developer) #5

Thats how my comment model looks like now. But the main problem Is rendering the deep level comments. Currently I am using for loop to render the comments but when it comes to replies I can’t render them using for loop because even through the IDs of parent comment is stored, I can’t use them while rendering the deep level comments because JavaScript don’t knows which id belongs to which comment as the parent comments are already rendered their ids don’t exist anymore in the context. However the solution to this is react but I don’t know how to pass variable from ejs(backend) to react(frontend)

(Not a java developer) #6

They loads so much unnecessary stuffs.

(Not a java developer) #7

I searched a lot but didn’t find anything! Ghost doesn’t comes with comment system. Keystone js has built in comment system but replies are not supported

(DND) #8

for rendering you can use multiple forms with differnt query string

i.e :

like if parent_id as a query string… so when new user reply to a comment
that new comment will be stored with parent comment_id and parent_id row … with child_id

this way you will always deal with single pair parent-child comment … doesn’t matter how deep

additionally you can store child_id in parent row … this will save some cpu resources in future
for every child comment set post_id = -1 because we don’t want to select in main loop
if no child_comment exist then child_id = -1 so here we can terminate rendering deeper comment

Now rendering comments:

run loop on Post_id … then just traverse using child_id until child_id = -1 now just increment loop variable and move to another parent_id

(Abhishek Pathak) #9

But I suppose there can be more than one child comments.

(Not a java developer) #10

But this way I have to make database call per id. If there is 20 replies then 20 database call. This might be inefficient

(DND) #11

even a normal 5$ vps can handel 30k queries/hour …

it will be fast untill and unless … you got rows count in millions

will you be making caching system also ?

(DND) #12

humm … this is where my logic will not work.

(Abhishek Pathak) #13

Why are you using ejs with React?
EJS is view engine for Node, which is nothing but for rendering front-end.

(Not a java developer) #14

beacuse the comment data will be extracted from the database when the server gets an GET request, now the comments data will be handover to view engine. But as i am having difficulty rendering the comments via ejs i am thinking to handover that comments data somehow to react and then in react i will do some checking to find out the child comments (replies) and then efficiently render it to the dom.


Okay I haven’t worked with Ghost let me tell you how I will achieve this in python with a MVT framework Django.

In my database models I will have a comment class Where I will have name, email, text and the common fields but apart from that I will have a field called Parent which will be foreign key column ( I hope you know what this is ) for a main comment it will be saved as null and will have some integer values for replies.

Now in views just make a simple form and check condition if the new comment has parent_id value or not, if it has save it to reply_comment variable.

And finally in your templates loop over the comment and replies.

(Not a java developer) #16

this is what my comment model looks like now:

const mongoose = require('mongoose');
CommentSchema = new mongoose.Schema({
name: String,
email: String,
comment: String,
approved: {type: String, default: "no"},
parent_id: {type: String,  default: "0"}
const comments = mongoose.model("Comments", CommentSchema);
module.exports = comments;

as you can see i already storing parent_id and “0” if not a reply. Storing comments is not a problem. the problem is rendering the comments into the DOM. now with mongoDB every new comment is stored as an object inside an array. So to render the comments I have to use for loop. So if a comment is a reply to a comment that is already rendered by the “for loop”. as the reply contains the id of its parent comment. i have to compare that id matches which parent comment id. But in ‘for loop’ how will you do this? i can use nested for loop but that will be inefficient i have a lots of comments as the nested for loop will run for every single comment.


I will use nested loops.

First loop for the comments in comment model second for replies in reply.

This way everything gets rendered.

PS - just saw your edited reply

(Not a java developer) #18

i can use nested for loop but that will be inefficient if i have a lots of comments as the nested for loop will run for every single comment.


You edited that later lol.

Anyways you can have a button for load comments which will only load comments if clicked just like meduim(dot)com that would be an efficient way.

(DND) #20

how many ?