简体   繁体   English

Socket.IO 是否可扩展?

[英]Does Socket.IO scale?

I really love the design of socket.io and I was planning on making a matchmaking system using it.我真的很喜欢 socket.io 的设计,我正计划使用它制作一个配对系统。 Everything I've done so far has worked so far.到目前为止,我所做的一切都奏效了。 However, I worry about scalability, what happens if I start having thousands of lobbies, with thousands of players connecting to it?但是,我担心可扩展性,如果我开始拥有数千个大厅,并且有数千名玩家连接到它,会发生什么? How can I ensure that socket.io will scale up to millions of connections, or at least hundreds of thousands?如何确保 socket.io 可以扩展到数百万或至少数十万的连接?

you can use UWebSocket.js for better performance and large scale program您可以使用UWebSocket.js获得更好的性能和大规模程序

see benchmarks查看基准

but you need to implement your communication protocol like socket.io, and you can implement it like this example (don't use this in production this is just a toy example)但是你需要实现你的通信协议,比如 socket.io,你可以像这个例子一样实现它(不要在生产中使用它,这只是一个玩具例子)

const Event = require('events')

class EventRouter extends Event {
    async process (msg) {
        this.emit(msg.event, msg.data)
    }
}


let router = new EventRouter()

// set up your connection and sockets

socket.on('message', router.process)

router.on('your route & your event ....', msg => {
    // your handler
})


function emit(socket, event, data) {
    socket.send({event, data})
}

and repeat this code for your client-side并为您的客户端重复此代码

Does socket.io Scale? socket.io 是否缩放? Sort of...有点...

Socket.io its self is about solving some of the thorny issues in managing sockets and fallbacks. Socket.io 本身是关于解决管理 sockets 和后备的一些棘手问题。 its great, and will get you up and running.它很棒,可以让您启动并运行。 However its not really socket.io you need to worry about with scale.然而,它并不是真正的 socket.io,您需要担心规模。 It just manages the sockets.它只管理 sockets。 at most its a bit less scalable than doing raw, but it's never the whole solution.最多它的可扩展性比原始的要小,但它永远不是完整的解决方案。 You'll normally run out of CPU or TCP connections first.您通常会先用完 CPU 或 TCP 连接。 This means you end up needing to go broad, and have more than one machine doing things at once.这意味着您最终需要 go 广泛,并且同时拥有多台机器做事。

You'll need more than just Socket.io to get to millions of connections.要获得数百万个连接,您需要的不仅仅是 Socket.io。 The most popular answer is using redis to handle the distribution and using something like kubernetes to handle the horizontal scale of frontend servers.最流行的答案是使用 redis 来处理分发,并使用 kubernetes 之类的东西来处理前端服务器的水平规模。

In my experience a lot of people seem to have issues getting above 30k connections (I work for Ably.io) but it is doable...以我的经验,很多人似乎在连接超过 30k 时遇到问题(我为 Ably.io 工作),但这是可行的......

It is a lot of work to manage all of these bits, and unless its actually core to your offering to own this tech, why not use AWS API gateway websockets or a hosted provider of sockets/pubsub?管理所有这些位需要大量工作,除非它实际上是您拥有这项技术的核心,否则为什么不使用 AWS API 网关 websockets 或套接字/pubsub 的托管提供商?

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

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