简体   繁体   中英

When to push data with Server-sent Events

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
}
  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM