简体   繁体   English

扩展 websocket 节点服务器

[英]Scaling websocket node server

I know this question has been asked partially before ( How to Scale Node.js WebSocket Redis Server? ) but I am wondering if there is any alternatives to redis for rapidly sharing websocket objects between node instances, specifically ws type sockets ( https://github.com/einaros/ws ).我知道之前已经部分地问过这个问题( How to Scale Node.js WebSocket Redis Server? ),但我想知道是否有任何替代 redis 可以在节点实例之间快速共享 websocket 对象,特别是 ws 类型的套接字( https:// github.com/einaros/ws )。 I've tried redis and ran into issues with the fact that the web socket objects are cyclic and difficult to serialise.我尝试过 redis 并遇到了 Web 套接字对象是循环的且难以序列化的问题。 I then used Crockford's cycle.js ( https://github.com/douglascrockford/JSON-js/blob/master/cycle.js ), however it seems to strip out the websocket objects methods, as I get an error from node saying "Object object has no method send" after I have read the socket back from redis and retrocycled it.然后我使用了 Crockford 的 cycle.js ( https://github.com/douglascrockford/JSON-js/blob/master/cycle.js ),但是它似乎去掉了 websocket 对象方法,因为我从节点收到错误说在我从 redis 读回套接字并对其进行回收后,“对象对象没有发送方法”。 Any help would be much appreciated.任何帮助将非常感激。

Thanks in advance, James.提前致谢,詹姆斯。

IMO you should use messaging queue for that.. eg (RabbitMQ) 海事组织,你应该为此使用消息队列。.例如(RabbitMQ)

  1. Application starts on Node A and Node B and connects to RabbitMQ 应用程序在节点A和节点B上启动并连接到RabbitMQ
  2. Client A connects to Node A and subscribe to Queue named XXX Client 客户端A连接到节点A并订阅名为XXX客户端的队列
  3. Client B connects to Node B and subscribe to Queue named XXX 客户端B连接到节点B并订阅名为XXX的队列
  4. Client A sendsmessage to websocket server Websocket Server sends message to Node A 客户端A将消息发送到Websocket服务器Websocket服务器将消息发送到节点A
  5. Node A publishes messages to RabbitMQ queue XXX 节点A将消息发布到RabbitMQ队列XXX
  6. Node B receives the message from RabbitMQ as it is subscribed to queue XXX 节点B从RabbitMQ收到消息,因为它已订阅队列XXX
  7. Node B sends message to Client B or publishes the messages to all connected clients on node B 节点B向客户端B发送消息或将消息发布到节点B上所有连接的客户端

So, all you need is to put Messaging queue in your architecture (RabbitMQ, ZeroMQ) etc 因此,您所需要做的就是将消息传递队列放入您的体系结构(RabbitMQ,ZeroMQ)等中

There is a library which allows easily scale WebSocket across node.js processes and machines, you can check out it: 有一个库可以轻松在node.js进程和机器之间扩展WebSocket,您可以签出:

https://github.com/ClusterWS/ClusterWS https://github.com/ClusterWS/ClusterWS

When we speak of scalability we expect or want to hear the words linear performance gains.当我们谈到可扩展性时,我们期望或希望听到线性性能提升这两个词。 To be honest though this is not the case most setups as their reliance on another server/service is too great and thus bottle-necks form up within the network you're trying to host for users.老实说,尽管大多数设置并非如此,因为它们对另一台服务器/服务的依赖太大,因此在您尝试为用户托管的网络中形成了瓶颈。

As we explore options we hear things like Databases, Message Queues, and Brokers;当我们探索选项时,我们会听到诸如数据库、消息队列和代理之类的东西; These are fine to use but as mentioned above if reliance on any of them is far too great you will destroy your setup in sure time.这些很好用,但如上所述,如果对它们中的任何一个过于依赖,你肯定会破坏你的设置。


Design the WSS Server to act solo (unless requirements are exceeded).将 WSS 服务器设计为单独运行(除非超出要求)。 You determine and set limits and let API server know this.您确定并设置限制,并让 API 服务器知道这一点。 So if I have 10 chat-rooms and they hold maximum 100 users and benching my WSS server proved I could hold 400-500 of them.因此,如果我有 10 个聊天室,并且它们最多可容纳 100 个用户,并且将我的 WSS 服务器放在板凳上证明我可以容纳 400-500 个用户。 With that information I'd set 4-5 rooms per server.有了这些信息,我会为每台服务器设置 4-5 个房间。 So if two people enter room#1 they are on WSS server#1;因此,如果两个人进入房间#1,他们就在 WSS 服务器#1 上; If all 10 chat-rooms are full then WSS server #2 is now full and 11th room will need a WSS Server#3 up to 15th room.如果所有 10 个聊天室都已满,那么 WSS 服务器 #2 现在已满,第 11 个房间将需要一个 WSS Server#3 直到第 15 个房间。

The slowest part of the network would now just be your API server handling requests but this may include database as well.网络中最慢的部分现在只是您的 API 服务器处理请求,但这也可能包括数据库。


If your requirements are for more users than the example, you can increase core power first or add a second server with help of an MQ or Redis Pub/Sub type setup.如果您的要求是比示例更多的用户,您可以首先增加核心能力或在 MQ 或 Redis Pub/Sub 类型设置的帮助下添加第二个服务器。


Unfortunately there's no way to properly sort users, so if 3 rooms had 20 users and all were sitting on WSS server#1 that'd still leave a room left with hundreds of user slots available but is this really a problem?不幸的是,没有办法正确地对用户进行排序,所以如果 3 个房间有 20 个用户并且所有用户都坐在 WSS 服务器#1 上,那么仍然会留下一个有数百个用户槽可用的房间,但这真的是一个问题吗?

It's possible this room could fill right up so leave them the spot, but still could be days till they max so programming something spicy for your needs will improve how cost effective you make it.这个房间有可能会被填满,所以让他们留在原地,但仍然可能需要几天时间才能达到最大,因此根据您的需要编写一些辛辣的东西将提高您制作它的成本效益。

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

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