Mass chat reactivity


#1

Hey guys!

I’ve created a Meteor chat-like app that is already being used in a couple online courses as a live interaction system between teachers and students.
I’ve used mongoDB, and realized that, for reactivity, it reloads all my chat comments when something is inserted in the DB. The issue is that, because there are a lot of students commenting at the same time, some comments get stuck at the end of the comments array - because of some mongoDB mass insertions from different clients I think - and are always shown first.

To solve this issue, I’ve thought of sorting the comments by date (something I thought was already being done by mongo), but I’m very concerned about performance, as it would sort it again for every user for every single new comment inserted.

Can anybody help me with this? Is this a real solution? Will this really affect the performance?

Thanks in advance!
Luis.


#2

That is the accepted solution. However, you will need to add an index to your date field, or sorting will seriously affect performance. Something like this in your server-side startup:

Meteor.startup(() => {
  ChatCollection._ensureIndex( {createdAt: 1} );
});

#3

Are your comments in a separate collection or are they added as an array in another object like course or chat?


#4

Well, I figured out what happened.
What I was doing was something like that (in my architecture, there’s a comments array in every chat object), but comments were still getting stuck. When I inspected the database, I found out that the “created at” field was being set eith the client date, not the server date. Because of that, and because there were people from different time zones using it, the array was a mess.
What I did to solve this was simply removing any index, and just relying on the array insertion order.