I'm storing data in a database and whenever it updates, It sends an event to the client side.
Here is server-side
socket.on("note delete", function(data) {
rooms
.findOneAndUpdate(
{
_id: data.roomId
},
{ $pull: { notes: { _id: data._id } } }
)
.then(docs => {
rooms.find({ _id: data.roomId }, function(err, docs) {
socket.emit("remainingnotes", docs);
});
})
.catch(err => {
console.log("err", err.stack);
});
remainingnotes is emitted on the client side. Now I want to update its content to this.state/MobX state real-time. as soon as database changes, I want my Store value to update accordingly.
How can I do this?
// Client Side
socket.on("remainingnotes", function(data) {
ChatStore.msgs = data[0].conversation; // ChatStore is my MobX store.
});
I've tried adding this event in Constructor and in ComponentDidMount but both didn't update. Is there any way to listen to events on the client side for React?