gizmo
gizmo

Reputation: 201

Inform Angular frontend when state in Node backend (MongoDB) changes

I want a real-time update on Angular frontend when state changes in MongoDB. What are my choices? Are there any other ways than, for ex. using socket.io?

Scenario - user creates a reminder, sets a date on which email will arrive in his mail box. Now for adding and removing reminders I can mimic real-time on front end side, because I know when user invokes some action that leads to a state change, but I also have a text next to every reminder - "Email was sent" or "Email not sent yet". Because I use cron jobs to send emails on my backend, front end cant know about it.

Its my first project where I code my backend, so I just wonder maybe there are some other ways than implementing lots of redundant stuff just to make one small feature to work?

NodeJs + Express + Angular6 + MongoDB

Upvotes: 2

Views: 1606

Answers (2)

Syed Kashan Ali
Syed Kashan Ali

Reputation: 663

Check Change Streams the newest and coolest feature of MongoDB 3.6 for accessing real time changes from database.

Sample Node.js Code:

const collection = db.collection('example');
const changeStream = collection.watch();
const next = await changeStream.next();

Upvotes: 5

vishnu s pillai
vishnu s pillai

Reputation: 101

You can implement a polling service using angular and rxjs. The service will check your database on a specific interval.

this.subscription = Observable.interval(1000).startWith(1)
.mergeMap(() => this.statusService.getStatus())
.subscribe((next) => {
 this.state = next;
 })

You can use rxjs interval operator for polling

Upvotes: 0

Related Questions