简体   繁体   English

带有Mongoose流的高效Socket.io分发

[英]Efficient Socket.io distribution with Mongoose stream

I'm trying to create an efficient streaming node.js app, where the server would connect to a stream (capped collection) in MongoDB with mongoose , and then emit the stream directly to the client browsers. 我试图创建一种高效的流node.js应用程序,其中,服务器将连接到一个stream中(加盖集合) MongoDBmongoose ,然后直接排放流给客户端的浏览器。

What I'm worried about is the scalability of my design. 我担心的是设计的可扩展性。 Let me know if I'm wrong, but it seems that right now, for every new web browser that is opened, a new connection to MongoDB will also be opened (it won't re-use the previously utilized connection), and therefore there will be a lot of inefficiencies if I have a lot of user connected at the same time. 如果我错了,请告诉我,但是现在看来,对于每个打开的新Web浏览器,也将打开与MongoDB的新连接(它将不重新使用以前使用的连接),因此如果我同时连接了许多用户,效率将会很低下。 How can I improve that? 我该如何改善呢?

I'm thinking of a one server - multiple client type of design in socket.io but I don't know how to achieve that. 我正在考虑一台服务器-socket.io中的多客户端设计,但是我不知道如何实现。

Code below: 代码如下:

server side (app.js) : 服务器端(app.js)

io.on('connection', function (socket) {
  console.log("connected!");
  var stream = Json.find().lean().tailable({ "awaitdata": true, numberOfRetries: Number.MAX_VALUE}).stream();
  stream.on('data', function(doc){
    socket.emit('rmc', doc);
  }).on('error', function (error){
    console.log(error);
  }).on('close', function () {
    console.log('closed');
  });
});

client side (index.html) : 客户端(index.html)

socket.on('rmc', function(json) {
  doSomething(); // it just displays the data on the screen
});

Unfortunately this will not depend only on mongo performance . 不幸的是,这不仅仅取决于mongo的性能。 unless you have a high level of concurrency (+1000 streams) you shouldn't worry about mongo (for the moment). 除非您具有较高的并发级别(+1000个流),否则您暂时不必担心mongo。

because with that kind of app you have bigger problems example: the data type and compression , buffer overflows , bandwith limit , socket.io limits , os limits . 因为使用这种应用程序您会遇到更大的问题,例如:数据类型和压缩,缓冲区溢出,带宽限制,socket.io限制,os限制。 These are the kind of problems you will most likely face first. 这些是您最有可能首先要面对的问题。

now to answer your question. 现在回答您的问题。 As far as i know no you are not opening a connection to mongo per user. 据我所知,您没有按用户打开与mongo的连接。 the users are connected to the app not the database . 用户连接到应用程序而不是数据库。 the app is connected with the database. 该应用程序已与数据库连接。

lastly , these links will help you understand and tweak your queries for this kind of job (streaming) 最后,这些链接将帮助您了解和调整对此类工作的查询(流式处理)

https://github.com/Automattic/mongoose/issues/1248 https://github.com/Automattic/mongoose/issues/1248

https://codeandcodes.com/tag/mongoose-vs-mongodb-native/ https://codeandcodes.com/tag/mongoose-vs-mongodb-native/

http://drewww.github.io/socket.io-benchmarking/ http://drewww.github.io/socket.io-benchmarking/

hope it helps ! 希望能帮助到你 !

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

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