Detect when new data is added


#1

Hello,

I have a simple application where user updates their status and other users can see that. At the first time it will show all the statuses by default. Of course if another user updates the status, the current user will get that status without refreshing the page. Now the question is how can I detect newly added data in my template ?

For example, Currently there are 4 statuses as “TEST 1”, “TEST 2”,“TEST 3” and “TEST 4”. Now if “TEST 5” is added, how do I log into the console that this data newly added.

Thanks!


#2

Hi Abhishek009,

Please provide some code that you already implemented, so that we can help you better.

If you want to log a newly added item to a collection, you can do the following:


Stats.find().observe({
  added(doc) {
    console.log("Stat added", doc);
  },
  changed(doc, oldDoc) {
    console.log("Stat changed to", doc);
  },
  removed(doc) {
    console.log("Stat removed", doc);
  }
});


#3

Thanks for your reply. The code you gave detects on server side. I want on the client side. For example, If other user posts the article and another user is already logged in. As meteor auto refreshes, the newly created blog post will directly appear on the page. I want to log out that new information that client just received. I hope you understand.


#4

@abhishek009,
Please read the docs, you also have observers on the client side. Just make sure the data is published properly.


#5

And how use with observers with React?