简体   繁体   English

何时使用服务器发送的事件推送数据

[英]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. 现在,我正在每10秒为连接的每个用户查询db以获取新记录。 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 . 前端/后端链接:您可以使用WebSockets this way data is transfered through TCP and not HTTP (faster and uses less ressources) 这样,数据通过TCP而不是HTTP传输(速度更快,使用的资源更少)
  • 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) 如果您可以修改插入数据的代码,则使其通过WebSockets(或根据需要通过HTTP,将这些数据发送到后端,但WebSocket更快)并更新记录数据库(或新连接的用户)

    If you can't, here's a link : How to listen for changes to a MongoDB collection? 如果不能,那么这里是一个链接: 如何收听对MongoDB集合的更改?

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.JS上的服务器发送事件 - Server-Sent Events on Node.JS 在服务器关闭连接之前,浏览器不响应服务器发送的事件 - Browsers are not responding to Server-Sent Events until server closes connection SSL/HTTPS 的服务器发送事件 (SSE) 问题 - Server-Sent Events (SSE) problem with SSL/HTTPS 服务器发送的事件不适用于 Azure 中的 Window 应用服务 - Server-sent Events not working with Window App Service in Azure Koa Server-sent Events仅向浏览器中的EventSource产生错误 - Koa Server-sent Events yields only errors to EventSource in browser 使用服务器发送的事件:如何存储与客户端的连接? - Using server-sent events : how to store a connection to a client? 无法在基于 Node ExpressJS 服务器的服务器发送事件中检索 req.body 的值? - Cannot retrieve value of req.body in server-sent events based Node ExpressJS Server? Node.js上的Server-Sent Events连接超时通过Nginx - Server-Sent Events connection timeout on Node.js via Nginx 使用 node.js http2 模块的服务器发送事件 - 我应该如何将它与流 / pushStream 一起使用? - Server-sent events with node.js http2 module - how should I use it with stream / pushStream? 为什么我的 Angular / Express.js 服务器发送事件代码不起作用? - Why does my Angular / Express.js Server-Sent Events Code not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM