Currently there is a plethora of websocket libraries for node.js, the most popular seem to be:

However I can't find any solid concrete comparisons between any of them... Apparently Socket.io was awesome, but has become quite dated and has failing builds. Both ws and websocket-node claim they are the fastest. And engine.io seems new, but a lot heavier than the lighter aletarntives.

It would be amazing if we or someone could put together an answer that serves as a guide on which socket library to use and when, as well as a comparison between them.

推荐答案

Getting the ball rolling with this community wiki answer. Feel free to edit me with your improvements.

  • ws WebSocket server and client for node.js. One of the fastest libraries if not the fastest one.

  • websocket-node WebSocket server and client for node.js

  • websocket-driver-node WebSocket server and client protocol parser node.js - used in faye-websocket-node

  • faye-websocket-node WebSocket server and client for node.js - used in faye and sockjs

  • socket.io WebSocket server and client for node.js + client for browsers + (v0 has newest to oldest fallbacks, v1 of Socket.io uses engine.io) + channels - used in stack.io. Client library tries to reconnect upon disconnection.

  • sockjs WebSocket server and client for node.js and others + client for browsers + newest to oldest fallbacks

  • faye WebSocket server and client for node.js and others + client for browsers + fallbacks + support for other server-side languages

  • deepstream.io clusterable realtime server that handles WebSockets & TCP connections and provides data-sync, pub/sub and request/response

  • socketcluster WebSocket server cluster which makes use of all CPU cores on your machine. For example, if you were to use an xlarge Amazon EC2 instance with 32 cores, you would be able to handle almost 32 times the traffic on a single instance.

  • primus Provides a common API for most of the libraries above for easy switching + stability improvements for all of them.

When to use:

  • use the basic WebSocket servers when you want to use the native WebSocket implementations on the clientside, beware of the browser incompatibilities

  • use the fallback libraries when you care about browser fallbacks

  • use the full featured libraries when you care about channels

  • use primus when you have no idea about what to use, are not in the mood for rewriting your application when you need to switch frameworks because of changing project requirements or need additional connection stability.

Where to test:

Firecamp is a GUI testing environment for SocketIO, WS and all major real-time technology. Debug the real-time events while you're developing it.

Node.js相关问答推荐

无法在我的 node 项目中转让Google Drive v3 API中的所有权

如何在不丢失其他键的情况下解开子文档数组,然后反转该过程?

MongoDB的方面查询的Postgres类似功能

MongooseError[MissingSchemaError]:尚未为模型注册架构

如果我加入另一个公会且我的​​机器人已在其中,欢迎消息发送错误

使用单个 MongoDB 查询更新多个元素

结合后端(Express)和前端(Angular)路由

我误解了外键的工作原理吗?使用续集

在 Header 中使用 Authorization 方法和新的附加参数:accessToken 有什么区别?

Zod 模式中的self 数组

当我使用 uuid 代码意外崩溃,然后工作正常?

类型中缺少属性,该类型是由类实现的接口

Socket IOFlutter 未连接

如何将`yarn.lock`与`package.json`同步?

如何为 node.js 服务器分配域名?

从 zip 文件在 AWS 中创建 lambda 函数

在 Node 中连接和缩小 JS 文件

使用 node 的内置调试器判断变量?

- configuration.output.path:提供的值public不是绝对路径!使用 Webpack

为什么 Node.js 没有原生 DOM?