目前有大量的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
然而,我找不到任何可靠的具体比较…
显然插座。IO是很棒的,但已经变得相当过时,并有失败的构建。ws和websocket-node都声称它们是最快的。和引擎。IO看起来很新,但比较轻的aletartives要重得多。
如果我们或者其他人能够给出一个答案,作为使用哪个套接字库以及何时使用的指南,以及它们之间的比较,那将是令人惊讶的。
从这个社区wiki的答案开始。请随意编辑我与您的改进。
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.
使用时间:
当你想在客户端使用本地WebSocket实现时,使用基本的WebSocket服务器,小心浏览器不兼容
当您关心浏览器回退时,请使用回退库
当您关心通道时,请使用功能完整的库
当您不知道使用什么,当您因为改变项目需求或需要额外的连接稳定性而需要切换框架时,没有心情重写应用程序时,请使用primus。
测试地点:
Firecamp是一个用于SocketIO、WS和所有主要实时技术的GUI测试环境。在开发时调试实时事件。
从这个社区wiki的答案开始。请随意编辑我与您的改进。
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.
使用时间:
当你想在客户端使用本地WebSocket实现时,使用基本的WebSocket服务器,小心浏览器不兼容
当您关心浏览器回退时,请使用回退库
当您关心通道时,请使用功能完整的库
当您不知道使用什么,当您因为改变项目需求或需要额外的连接稳定性而需要切换框架时,没有心情重写应用程序时,请使用primus。
测试地点:
Firecamp是一个用于SocketIO、WS和所有主要实时技术的GUI测试环境。在开发时调试实时事件。
更新:这个答案已经过时了,因为之前提到的库的更新版本已经发布了。
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
NPM ws就是我的答案。我发现它不那么无礼,更直接。将websocket与rest服务混合使用也很简单。分享这篇文章的简单代码。
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");