简体   繁体   English

如何在Node.JS中处理WebSocket服务器上的巨大流量?

[英]How to handle huge traffic on WebSocket server in Node.JS?

I use ws library in Node.JS. 我在Node.JS中使用ws库。 When I've got more than 100 clients, the WebSocket server can't handle all traffic, the server lags, but if users are disconnected, the server works correctly. 当我有超过100个客户端时,WebSocket服务器无法处理所有流量,服务器将滞后,但是如果用户断开连接,则服务器可以正常工作。 I use Heroku to host servers. 我使用 Heroku托管服务器。 What can I do for optimize my servers/app? 如何优化服务器/应用程序? Should I use another service than Heroku ? 我应该使用 Heroku以外的其他服务吗?

\n

EDIT: I'm using paid version of Heroku (Hobby). 编辑:我正在使用付费版本的Heroku(业余爱好)。 My script isn't long (about 140 lines). 我的脚本不长(大约140行)。

EDIT: I have 3 x VPS Cloud 3 from OVH. 编辑:我有3 x OVH的VPS Cloud 3。

  • OpenStack KVM OpenStack KVM
  • 99.99% SLA SLA 99.99%
  • 4 vCores 4个vCore
  • 3.1 GHz 3.1 GHz
  • 8 GB RAM 8 GB RAM
  • 100 GB 100 GB
  • High Availability NVMe (Ceph) 高可用性NVMe(Ceph)
  • Bandwidth 100 Mbps 带宽100 Mbps
  • Ubuntu Server 15 Ubuntu服务器15
  • 17 IPs per server 每个服务器17个IP

Client -> VPS 1 -> ((VPS 2 -> example.com) + (VPS 3 -> example.com)) -> VPS 1 -> Client 客户端-> VPS 1->(((VPS 2-> example.com)+(VPS 3-> example.com))-> VPS 1->客户端

This question does not contain enough information to properly answer the question. 该问题没有足够的信息来正确回答该问题。 There are a lot of factors that can contribute to this. 有很多因素可以促成此。 To name a few: 仅举几例:

  • Machine spec's (memory, cpu etc) 机器规格(内存,CPU等)
  • IO (HDD speed) IO(硬盘速度)
  • File Descriptors 文件描述符
  • Code (multi-core, threads etc) 代码(多核,线程等)
  • Bandwidth 带宽
  • Networking constraints 网络约束

As someone else stated, Node.JS is typically running on a single core with asynchronous IO. 如其他人所述,Node.JS通常在具有异步IO的单个内核上运行。 Concurrency, as I've argued with many people in the past, is not parallelism. 正如我过去与许多人争论的那样,并发不是并行性。 There is a huge difference. 这是个很大的差异。 That doesn't mean Node couldn't be used to support more users, but it also depends on how you've implemented it and all of the other factors lying underneath. 这并不意味着不能使用Node来支持更多的用户,但这还取决于您如何实现它以及所有其他潜在因素。 Think of a car that simply won't start. 想想一辆根本无法启动的汽车。 You can't just say, "Hey, my car won't start - why not?" 您不能只说:“嘿,我的车无法启动-为什么不呢?” At the same time, you can't simply say, "Hey, my car starts, but sometimes when driving, it vibrates badly and makes funny noises." 同时,您不能简单地说:“嘿,我的汽车起步了,但有时在开车时,它剧烈振动并发出有趣的声音。”

Can you put the Node.JS web socket code you're using up on a pastebin, and also include information on the server it's running on? 您是否可以将正在使用的Node.JS Web套接字代码放在pastebin上,并且还可以在其运行的服务器上包含信息? Some good info from there would be: 从那里可以得到一些很好的信息:

cat /proc/meminfo cat /proc/cpuinfo ulimit -a cat / proc / meminfo cat / proc / cpuinfo ulimit -a

Also, the bandwidth you have (100mb? 10mb?) 另外,您拥有的带宽(100mb?10mb?)

Those would be good starting grounds to look at the problem and try and determine where the bottleneck is. 这些将是查看问题并尝试确定瓶颈所在的良好起点。

Oh, if you're on an OpenVZ host, it would be useful if you could give the bean counter information. 哦,如果您使用的是OpenVZ主机,则可以提供bean计数器信息会很有用。

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

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