Tag: 服务器发送的事件

SSE(服务器发送事件),在Node.js服务器中监听事件并将其发送到其他服务器

我有通过SSE(服务器发送事件)触发的事件监听器,我怎么可以将数据发布到其他服务器。 NODE JS //including Event Source var EventSource = require('eventsource'); var es = new EventSource('http://api.xyz.com:8100/update/'); //Listening URL Event Sourse es.addEventListener('message', function (e) { //Extract Json var extractData = JSON.parse(e.data); if(extractData.type == 'CALL' ) { console.log(extractData); //POST DATA } }); 我需要将数据发布到其他服务器的另一端是PHP

了解如何通过Node.js和Server Sent Events使用Redis

我的项目使用Nodejs作为代理服务器与外部API进行通信。 API通过Redis发送产品更新(pub / sub); 代理服务器处理消息并通过SSE(服务器发送事件)将其发送给客户端。 这是我第一次使用Redis和SSE,在网上寻找教程似乎很容易实现,我做到了。 在客户端,我刚刚创build了一个EventSource ,一旦我收到更新,我会用它做一些事情: // Client Side var source = new EventSource('/redis'); // /redis is path to proxy server source.addEventListener('items', handleItemsCallback, false); source.addEventListener('users', handleUsersCallback, false); source.addEventListener('customers', handleCustomersCallback, false); // Function sample… function handleItemsCallback (msg) { // Do something with msg… } 在代理服务器中,我创build了一个路由到/redis的控制器来处理Redis消息: exports.redisUpdates = function (req, res) { // Redis Authentication […]

事件stream请求不发射客运阿帕奇下的closures事件

所以我在我的express js节点应用程序中有一个事件stream。 这里有一个概述: app.get('/eventstream', function(req, res){ req.socket.setTimeout(Infinity); res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' }); res.write('/n'); req.on('close', function(){ console.log('connection closed'); }); } 在我的本地开发框中,从命令行运行 node app.js 它工作正常,并打印出“连接closures”,当我closures我的浏览器中的标签。 但是,在我的服务器上运行,在Passenger的Apache下,它不会打印出任何消息 – 服务器似乎不会触发“closures”事件。 我正在使用此事件从我的活跃用户数中删除。 有任何想法吗? 干杯,丹

如何在express.js中使用服务器发送的事件

我用express.js设置了我的REST服务器。 现在我想添加sse到这个服务器。 在我实现这个 sse包之后,我得到一个错误。 我知道我得到这个错误,什么时候会尝试使用res.send两次,但我不是。 ERROR: Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (http.js:690:11) at ServerResponse.header (/home/root/node_modules/express/lib/response.js:718:10) at ServerResponse.send (/home/root/node_modules/express/lib/response.js:163:12) at app.get.str (/home/root/.node_app_slot/main.js:1330:25) at Layer.handle [as handle_request] (/home/root/node_modules/express/lib/router/layer.js:95:5) at next (/home/root/node_modules/express/lib/router/route.js:131:13) at sse (/home/root/node_modules/server-sent-events/index.js:35:2) at Layer.handle [as handle_request] (/home/root/node_modules/express/lib/router/layer.js:95:5) at next (/home/root/node_modules/express/lib/router/route.js:131:13) at Route.dispatch (/home/root/node_modules/express/lib/router/route.js:112:3) 是否有可能我不能在sse函数中使用express方法? 例如: app.get('/events', sse, function(req, res) { […]

如何使用Expressclosuresnode.js中的HTTP连接

我使用node.js(Express)和服务器发送的事件。 我想通过closuressse HTTP连接来closures事件stream。 鉴于folllwingfunction: router.get('/sse', function (req, res) { }); 这怎么能实现?

在Node.js中实现服务器发送事件的简单方法?

我环顾四周,似乎在Node.js中实现SSE的所有方法都是通过更复杂的代码,但似乎应该有一个更简单的方式来发送和接收SSE。 是否有任何API或模块,使这更简单?

HAProxy不保持HTTP连接打开

我有一个Node.js服务器使用服务器发送事件(SSE)来允许推送通知连接的Web客户端。 当浏览器直接与节点对话时,它工作的很好。 但是,当我把haproxy放在中间时,为了达到其他要求,生产线必须每隔30秒closures并重新打开(感谢SSE的自动重新连接)。 我已经改变了,并尝试了我所知道的一切,并可以在haproxyconfiguration中在线find。 大多数信息都在那里,在他们的文档示例中涉及套接字,但是对于SSE的支持却很less。 它应该支持SSE的持久HTTP连接吗? 如果是的话,configuration它有什么窍门? 我的configuration如下:全局守护进程#最大并发连接数maxconn 4096#删除端口绑定后的权限用户nobody组nogroup#在文件中存储进程的pid pidfile /var/run/haproxy.pid#为stats stats创build此套接字socket /无功/运行/socketsHAProxy的 defaults log global mode http # disable logging of null connections option dontlognull # I've tried all these to no avail #option http-server-close #option httpclose option http-keep-alive # Add x-forwarded-for header to forward clients IP to app option forwardfor # maximum time […]

我想在点击一个button时在node.js服务器上触发一次server-sent-event

我想要做一个这样的过程: 用户selectcombobox菜单,然后单击“提交”button。 node.js服务器上的路由器路由来自客户端的请求。 路由器做了什么: 从客户端获取信息 查询数据库并获取数据。 用数据发送一个'draw_chart'事件stream到客户端html页面。 所以页面上的函数被使用来自服务器的数据触发。 所以我在这样的客户端html页面上创build了一个表单 chart.html: <form action = "http://localhost:1337/draw_chart" method = "GET"> <select id = "xAxisList" name = "xAxisList"></select> <select id = "yAxisList" name = "yAxisList"></select> <input type = "submit" id = "create_chart">Click to Draw a Chart</input> </form> 并使用express模块​​创build了一个路由器,以便在node.js服务器上路由来自客户端的请求。 server.js: function draw_chart_handler(req,res){ res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache' }); var […]

服务器端事件(SSE)没有到达客户端

我正在使用MEAN,我试图从服务器端接收事件。 为此,我使用EventSource,但它不工作。 我看到连接是如何打开的,但是我没有收到来自服务器的消息。 我可以看到在节点控制台如何发送消息,但在客户端没有什么(浏览器控制台)。 我有点迷路,因为我按照我find的所有教程,但使用完全相同的代码,它不工作。 在客户端,这是我的AngularJS代码: var source = new EventSource('/api/payments/listen'); source.addEventListener('open', function(e) { console.log('CONNECTION ESTABLISHED'); }, false); source.addEventListener('message', function (e) { $scope.$apply(function () { console.log('NOTIFICATION'); console.log(e.data); }); }, false); source.onmessage = function (e) { console.log('NOTIFICATION!'); console.log(e); }; source.addEventListener('error', function(e) { if (e.readyState === EventSource.CLOSED) { console.log('CONNECTION CLOSED'); } }, false); 服务器端代码: exports.listen = function […]

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

我想在新logging添加到数据库时stream式传输数据。 现在,我正在每隔10秒查询db的新logging为每个连接的用户。 有没有更好的方法来做到这一点? 我觉得我应该在插入新logging后提出事件,并以某种方式访问​​相关用户和用户的请求。 (我已经简化了我的代码,使其清晰明了,所以可能还没有完全正常工作。) 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 () { […]