简体   繁体   English

Node.JS上的服务器发送事件

[英]Server-Sent Events on Node.JS

I'm actually trying to create a web-application which will utilizes the Server-Sent Events draft . 我实际上正在尝试创建一个将使用Server-Sent Events草案的Web应用程序。 From my knowledge, SSEs utilize one thread per connection, and since the server is going to continuously pump data to the client, without being idle even for a second, there's no way I'll be able to put the thread back in the pool. 根据我的知识,SSE每个连接使用一个线程,并且由于服务器将连续将数据泵送到客户端,即使一秒钟也没有闲置,我无法将线程放回池中。

Hence I'm trying to use Node.JS (which I haven't used till date) to handle connections to the server. 因此,我正在尝试使用Node.JS(我还没有使用到日期)来处理与服务器的连接。 I've been through the HTML5 Rocks introduction to SSE and there is a code sample integrating SSEs with Node.JS. 我已经浏览了HTML5 Rocks对SSE的介绍,并且有一个代码示例将SSE与Node.JS集成在一起。

However, I'm confused as to whether Node.JS will handle the thousands of client connections concurrently and utilize the server more efficiently than an Apache server ? 但是,我很困惑Node.JS是否会同时处理数千个客户端连接并比Apache服务器更有效地利用服务器 Can anyone help me understand how exactly Node will act here? 任何人都可以帮助我理解Node将如何在这里行动吗?

Sorry if I sound a bit too vague. 对不起,如果我听起来有点太模糊。 I'm ready to make as many clarifications as possible! 我准备尽可能多的澄清! Thanks! 谢谢!

php: PHP:

do {
  sendMsg($startedAt , time());
  sleep(5);
} while(true);

vs VS

node.js 的node.js

setInterval(function() {
  constructSSE(res, id, (new Date()).toLocaleTimeString());
}, 5000);

The difference it the sleep blocks the php thread 5 seconds. 睡眠阻止php线程5秒的区别。 During those 5 seconds the server needs to have a dedicated thread doing absolutely nothing. 在这5秒钟内,服务器需要有专门的线程,什么都不做。 One thread per user. 每个用户一个线程。

With the node.js version the setInterval doesn't block the thread. 对于node.js版本,setInterval不会阻塞该线程。 The one node.js thread can handle all the users. 一个node.js线程可以处理所有用户。

Try to look at Understanding the node.js event loop article regarding concurrent connections. 尝试查看有关并发连接的了解node.js事件循环文章。 I would recommend to create a web application which utilizes WebSockets rather then Server-sent events because SSEs are less supported by browsers than WebSockets. 我建议创建一个使用WebSockets而不是服务器发送事件的Web应用程序,因为浏览器支持SSE的次数少于WebSockets。 Also there are lots of WebSockets based node.js modules with source codes at GitHub which can "inspire" you. 还有很多基于WebSocket的node.js模块,在GitHub上有源代码,可以“启发”你。

However, I'm confused as to whether Node.JS will handle the thousands of client connections concurrently and utilize the server more efficiently than an Apache server? 但是,我很困惑Node.JS是否会同时处理数千个客户端连接并比Apache服务器更有效地利用服务器? Can anyone help me understand how exactly Node will act here? 任何人都可以帮助我理解Node将如何在这里行动吗?

I think you should read Understanding event loops and writing great code for Node.js to get a better grasp about the event-loop. 我认为您应该阅读了解事件循环并为Node.js编写优秀代码以更好地掌握事件循环。 In node.js nothing is blocking which will save you tons of CPU-cycles. 在node.js中没有任何阻塞可以节省大量的CPU周期。

Also TJ's ebook can help you grasp events. TJ的电子书也可以帮助您掌握事件。 When something happens your callback associated with that event will be called. 当某些事情发生时,将调用与该事件相关的回调。

Try using express + connect-sse : 尝试使用express + connect-sse

var sse, express, app;

sse = require('connect-sse')();
express = require('express')

app = express()
app.get('/events', sse, function (req, res) {
  res.json("this is an event");
  res.json({here: "is", another: "event"});
});

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

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