452

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.

Igor Antun
  • 116
  • 1
  • 2
  • 14
balupton
  • 47,113
  • 32
  • 131
  • 182
  • 262
    If this gets closed, where should a question like this go? As the answer will be incredibly useful... Seems unfortunate that such a prominent question shouldn't reside here. – balupton May 06 '13 at 05:17
  • 16
    Woot woot for community wikis, meaning that despite the question being closed from new answers, we can still improve the community wiki answer below :) – balupton May 06 '13 at 07:10
  • 17
    I agree, I would like to see this kind of question not just allowed, but encouraged. OK, they may not be relevant in a years time, but till then they will save the world. – John Little Jul 21 '13 at 09:43
  • 1
    @balupton can you update the community with your choice and if you are happy with it? is it socket.io? – C graphics Oct 11 '13 at 18:20
  • 3
    @Cgraphics I use Primus with ws these days, as ws is all I needed, if I need something more extreme, I just swap out the undering library and keep the same api thanks to Primus. Works wonders. – balupton Oct 14 '13 at 01:21
  • For what it's worth, Socket.IO is solid. If you need browser compatibility, I recommend it. If you're looking for something pure websocket, use something else. – Brad Oct 27 '13 at 03:43
  • 1
    if interested, heres why this got closed: https://meta.stackoverflow.com/questions/291052/should-framework-comparison-questions-be-deleted/291129#291129 ( i think) – phil294 Jul 21 '17 at 13:34
  • How do questions like this get so many upvotes when its obviouly subjective and opinion based..? – JΛYDΞV Jun 17 '20 at 10:52

3 Answers3

416

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.

Felipe Plets
  • 7,230
  • 3
  • 36
  • 60
balupton
  • 47,113
  • 32
  • 131
  • 182
  • 10
    Thanks for the detailed answer. Can you explain point 3 in when to use. What are the channels that the point is referring to? I am new to websockets and am trying to understand which one to use in my case. – Akshat Jiwan Sharma Oct 03 '13 at 09:16
  • 2
    What about Autobahn/WAMP? – Emile Cormier Apr 03 '14 at 20:51
  • 1
    Nice list of all available option after death of now.js – Rahul Prasad Jun 15 '14 at 22:19
  • @AkshatJiwanSharma: Channels is the so called 'rooms' each player in a room can exchange messages.. – Marwen Trabelsi Aug 01 '14 at 02:44
  • 5
    A small detail for [ws](https://github.com/einaros/ws) is that it needs python. So if you deploy on docker containers or something, you'll need to have python available to use `ws`. – AntouanK Mar 08 '15 at 14:00
  • @EmileCormier WAMP is a subprotocol of WebSocket, not a WS library. op asked specifically about the latter. – Madbreaks Mar 25 '16 at 18:59
  • @AkshatJiwanSharma -- me too: "what do you mean by 'channels'"? Maybe it's the **concept** of a channel in the context of a messaging system, as seen on [this JBoss site](https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.0/html/Implementing_Enterprise_Integration_Patterns/files/MsgSys-MsgCh.html) "different message channels provides an elementary way of sorting messages into different message types." I still dunno what it means in context of WebSockets, except for this [websocket-rails doc](https://github.com/websocket-rails/websocket-rails/wiki/Working-with-Channels) – Nate Anderson Mar 15 '17 at 07:52
  • Deepstream.io server no longer works on Node.js, you now have to run it as a separate service and then add a Deepstream.io client onto Node.js to communicate with the server. – Luke Brown Jul 11 '17 at 09:59
  • I've tried `ws`, and it doesn't seem to work very well for streaming. If it falls behind on messages, it groups them into one and then acts as if it was posted multiple times. You can write code to figure out when it's doing that and ignore messages, or use something else. – Seph Reed Jul 31 '17 at 17:59
41

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

Deniz Ozger
  • 2,565
  • 23
  • 27
  • This reminds me of the usual *Node.js 0.12 is immediate* … they also claim that for months now. So, probably, we'll have to wait and see… – Golo Roden Mar 13 '14 at 11:10
  • 1
    Do you have any insights/experience with Autobahn/WAMP (http://wamp.ws/)? – Emile Cormier Apr 04 '14 at 14:35
  • 6
    Socket.IO released its v1. Engine.io is not a replacement for it, but used internally by Socket.IO as the transport protocol - it abstracts the actual communication between endpoints (xhr,ws) and has a simple format for packets. Socket.io provides a richer api that 'feels' like an event emitter over the wire. – hammett Jun 30 '14 at 20:07
34

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.

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");
MFAL
  • 1,090
  • 13
  • 19
  • How to get request url in connection metod? wss.on("connection", function (ws) { // some code console.log(ws.???) get url... } – Opalosolo Apr 09 '15 at 12:29
  • there is no request url as in ajax requests. The javascript library open a connection to your web-socket server using the an address such 127.0.0.1:8080 or anything.com:7777 , etc... . It is sounds confusing, try to go through some simple tutorial on web-sockets. – MFAL Jul 05 '15 at 11:59
  • 7
    Actually WebSocket endpoints have url too, they start with `ws`, such as `ws://myserver.com`. And yes, they can be configured to be served using 80 and 443 ports too. Using a reverse proxy like nginx, you can route the `WebSocket` protocol urls to another port from the normal HTTP protocols. – Prahlad Yeri Dec 05 '15 at 22:54
  • you can simulate routes if you want it. – Lucas Tettamanti Dec 29 '16 at 14:34
  • 1
    using `express-ws` allows to add `ws` routes in express app. – Dmitry Masley Jan 11 '17 at 09:41
  • upvoted! how would you test if this code works or not? – PirateApp Oct 29 '20 at 07:51