3

I want to stream data when new records are added to database. Now, I'm querying db for new records every 10 seconds for each of users connected. Is there any better way to do that? I feel like I should raise en event after new record is inserted and somehow access the request of the related user and stream for user.

(I have simplified my code to make point clear. So, probably it's not fully working.)

router.get("/event",  (req, res, next) => {
  let userId = getUserIdFromRequest(req);
  getData(userId, (err, data) => {
    let stream = res.push("/notification/new");
    stream.end(JSON.stringify(data));
    res.write("data:/notification/new\n\n");
    res.flush();
  });
});

getData(userId, callback) {
 model.find( {where: { and: [{ "userId": userId }, { "notified": false }] }}, (err, data =>) {
   callback(null , data);
   setTimeout(function () { getData(callback); }, 10000);
   if (data) {
     setDataAsNotified(data); // makes notified: true
   } 
 })
}

Frontend:

let source = new EventSource("/route/notification/event")
source.onmessage = (event) => {
 // display notification
}
Cory
  • 929
  • 12
  • 19

1 Answers1

0
  • Frontend/Backend link : you can use WebSockets. this way data is transfered through TCP and not HTTP (faster and uses less ressources)
  • Database Query : Since you didn't provided how you input data int the base, here are two leads :

    If you can modify the code inserting data, then make it send those data to the backend through WebSockets (or via HTTP as you want, but WebSocket are faster) and update the database for the record (or newly connected users)

    If you can't, here's a link : How to listen for changes to a MongoDB collection?

Hope this helps

Community
  • 1
  • 1
Robert
  • 66
  • 4
  • First bullet is incorrect: SSE and WebSockets will be as quick as each other (it is all just TCP/IP packets). (Also, OP wasn't asking for a quicker alternative to SSE, but when/how to reduce *upstream* latency.) – Darren Cook May 14 '17 at 08:07