哪个 websocket 库与 Node.js 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16392260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which websocket library to use with Node.js?
提问by balupton
Currently there is a plethora of websocket librariesfor node.js, the most popular seem to be:
目前有大量用于 node.js的 websocket 库,最流行的似乎是:
- https://github.com/Worlize/WebSocket-Node
- https://github.com/einaros/ws
- https://github.com/LearnBoost/engine.io
- https://github.com/learnboost/socket.io
- https://github.com/sockjs
- https://github.com/Worlize/WebSocket-Node
- https://github.com/einaros/ws
- https://github.com/LearnBoost/engine.io
- https://github.com/learnboost/socket.io
- https://github.com/sockjs
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.
但是,我找不到它们之间任何可靠的具体比较......显然 Socket.io 很棒,但已经过时并且构建失败。ws 和 websocket-node 都声称它们是最快的。engine.io 看起来很新,但比轻量级的 alantives 重很多。
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.
如果我们或某人可以将答案放在一起,作为使用哪个套接字库和何时使用的指南,以及它们之间的比较,那将是令人惊奇的。
采纳答案by balupton
Getting the ball rolling with this community wikianswer. Feel free to edit mewith your improvements.
wsWebSocket server and client for node.js. One of the fastest libraries if not the fastest one.
websocket-nodeWebSocket server and client for node.js
websocket-driver-nodeWebSocket server and client protocol parser node.js - used in faye-websocket-node
faye-websocket-nodeWebSocket server and client for node.js - used in faye and sockjs
socket.ioWebSocket 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.
sockjsWebSocket server and client for node.js and others + client for browsers + newest to oldest fallbacks
fayeWebSocket server and client for node.js and others + client for browsers + fallbacks + support for other server-side languages
deepstream.ioclusterable realtime server that handles WebSockets & TCP connections and provides data-sync, pub/sub and request/response
socketclusterWebSocket 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.
primusProvides a common API for most of the libraries above for easy switching + stability improvements for all of them.
wsWebSocket 服务器和 node.js 客户端。如果不是最快的库,也是最快的库之一。
websocket-node用于 node.js 的 WebSocket 服务器和客户端
websocket-driver-nodeWebSocket 服务器和客户端协议解析器 node.js - 在 faye-websocket-node 中使用
faye-websocket-node用于 node.js 的 WebSocket 服务器和客户端 - 在 faye 和 sockjs 中使用
socket.io用于 node.js 的 WebSocket 服务器和客户端 + 用于浏览器的客户端 +(v0 具有最新到最旧的回退,Socket.io 的 v1 使用 engine.io)+ 通道 - 在 stack.io 中使用。客户端库尝试在断开连接时重新连接。
sockjsWebSocket 服务器和用于 node.js 和其他的客户端 + 用于浏览器的客户端 + 最新到最旧的回退
fayeWebSocket 服务器和 node.js 和其他客户端 + 浏览器客户端 + 回退 + 支持其他服务器端语言
deepstream.io 可集群实时服务器,可处理 WebSockets 和 TCP 连接并提供数据同步、发布/订阅和请求/响应
socketclusterWebSocket 服务器集群,它利用您机器上的所有 CPU 内核。例如,如果您要使用具有 32 个内核的 xlarge Amazon EC2 实例,您将能够在单个实例上处理几乎 32 倍的流量。
primus为上面的大多数库提供了一个通用的 API,以便于切换和改进所有库的稳定性。
When to use:
何时使用:
use the basic WebSocket servers when you want to use the native WebSocket implementations on the clientside, beware of the browser incompatabilities
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.
当您想在客户端使用本机 WebSocket 实现时,请使用基本的 WebSocket 服务器,注意浏览器不兼容
当您关心浏览器回退时使用回退库
当您关心频道时使用功能齐全的库
当您不知道要使用什么时,请使用 primus,当您由于项目需求变化或需要额外的连接稳定性而需要切换框架时,没有心情重写您的应用程序。
Where to test:
测试地点:
Firecampis a GUI testing environment for SocketIO, WS and all major real-time technology. Debug the real-time events while you're developing it.
Firecamp是一个用于 SocketIO、WS 和所有主要实时技术的 GUI 测试环境。在开发实时事件时调试它。
回答by Deniz Ozger
Update: This answer is outdated as newer versions of libraries mentioned are released since then.
更新:此答案已过时,因为从那时起发布了所提到的库的新版本。
Socket.IO v0.9 is outdated and a bit buggy, and Engine.IO is the interim successor. Socket.IO v1.0 (which will be released soon) will use Engine.IO and be much better than v0.9. I'd recommend you to use Engine.IO until Socket.IO v1.0 is released.
"ws" does not support fallback, so if the client browser does not support websockets, it won't work, unlike Socket.IO and Engine.IO which uses long-polling etc if websockets are not available. However, "ws" seems like the fastest library at the moment.
See my article comparing Socket.IO, Engine.IO and Primus: https://medium.com/p/b63bfca0539
Socket.IO v0.9 已经过时并且有一些问题,Engine.IO 是临时继任者。Socket.IO v1.0(即将发布)将使用Engine.IO,比v0.9好很多。我建议您在 Socket.IO v1.0 发布之前使用 Engine.IO。
"ws" 不支持回退,因此如果客户端浏览器不支持 websockets,它将无法工作,不像 Socket.IO 和 Engine.IO 如果 websockets 不可用则使用长轮询等。然而,“ws”似乎是目前最快的库。
请参阅我比较 Socket.IO、Engine.IO 和 Primus 的文章:https://medium.com/p/b63bfca0539
回答by MFAL
npm ws was the answer for me. I found it less intrusive and more straight forward. With it was also trivial to mix websockets with rest services. Shared simple code on this post.
npm ws 是我的答案。我发现它不那么具有侵入性,而且更直接。将 websockets 与休息服务混合使用也很简单。在这篇文章中分享了简单的代码。
var WebSocketServer = require("ws").Server;
var http = require("http");
var express = require("express");
var port = process.env.PORT || 5000;
var app = express();
app.use(express.static(__dirname+ "/../"));
app.get('/someGetRequest', function(req, res, next) {
console.log('receiving get request');
});
app.post('/somePostRequest', function(req, res, next) {
console.log('receiving post request');
});
app.listen(80); //port 80 need to run as root
console.log("app listening on %d ", 80);
var server = http.createServer(app);
server.listen(port);
console.log("http server listening on %d", port);
var userId;
var wss = new WebSocketServer({server: server});
wss.on("connection", function (ws) {
console.info("websocket connection open");
var timestamp = new Date().getTime();
userId = timestamp;
ws.send(JSON.stringify({msgType:"onOpenConnection", msg:{connectionId:timestamp}}));
ws.on("message", function (data, flags) {
console.log("websocket received a message");
var clientMsg = data;
ws.send(JSON.stringify({msg:{connectionId:userId}}));
});
ws.on("close", function () {
console.log("websocket connection close");
});
});
console.log("websocket server created");

