Convert Figma logo to code with AI

uNetworking logouWebSockets.js

μWebSockets for Node.js back-ends :metal:

7,833
569
7,833
19

Top Related Projects

21,534

Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js

60,879

Realtime application framework (Node.JS server)

A WebSocket Implementation for Node.JS (Draft -08 through the final RFC 6455)

4,472

:zap: Primus, the creator god of the transformers & an abstraction layer for real-time to prevent module lock-in.

22,033

Package gorilla/websocket is a fast, well-tested and widely used WebSocket implementation for Go.

Quick Overview

uWebSockets.js is a high-performance WebSocket and HTTP server library for Node.js. It's built on top of the C++ library uWebSockets and provides a Node.js wrapper, offering exceptional speed and efficiency for real-time applications and web services.

Pros

  • Extremely high performance, capable of handling millions of connections
  • Low memory footprint compared to other Node.js WebSocket libraries
  • Supports both WebSocket and HTTP protocols
  • Easy integration with existing Node.js applications

Cons

  • Less extensive documentation compared to more established libraries
  • Fewer high-level abstractions, which may require more low-level coding
  • Limited community support and ecosystem compared to alternatives like Socket.io
  • May require more careful error handling due to its low-level nature

Code Examples

  1. Creating a WebSocket server:
const uWS = require('uWebSockets.js');

const app = uWS.App().ws('/*', {
  open: (ws) => {
    console.log('A WebSocket connected!');
  },
  message: (ws, message, isBinary) => {
    ws.send(message, isBinary);
  },
  close: (ws, code, message) => {
    console.log('WebSocket closed');
  }
}).listen(9001, (listenSocket) => {
  if (listenSocket) {
    console.log('Listening to port 9001');
  }
});
  1. Handling HTTP requests:
const uWS = require('uWebSockets.js');

const app = uWS.App().get('/', (res, req) => {
  res.writeHeader('Content-Type', 'text/html');
  res.end('<h1>Hello World!</h1>');
}).listen(9001, (listenSocket) => {
  if (listenSocket) {
    console.log('Listening to port 9001');
  }
});
  1. Broadcasting messages to all WebSocket clients:
const uWS = require('uWebSockets.js');

const app = uWS.App();
let sockets = new Set();

app.ws('/*', {
  open: (ws) => {
    sockets.add(ws);
  },
  close: (ws) => {
    sockets.delete(ws);
  }
});

function broadcast(message) {
  sockets.forEach(socket => {
    socket.send(message);
  });
}

app.listen(9001, (listenSocket) => {
  if (listenSocket) {
    console.log('Listening to port 9001');
  }
});

Getting Started

  1. Install uWebSockets.js:

    npm install uWebSockets.js
    
  2. Create a new file (e.g., server.js) and add the following code:

    const uWS = require('uWebSockets.js');
    
    const app = uWS.App().ws('/*', {
      open: (ws) => {
        console.log('A WebSocket connected!');
        ws.send('Welcome!');
      },
      message: (ws, message, isBinary) => {
        console.log('Received:', message);
        ws.send(message, isBinary);
      }
    }).listen(9001, (listenSocket) => {
      if (listenSocket) {
        console.log('Listening to port 9001');
      }
    });
    
  3. Run the server:

    node server.js
    
  4. Connect to the WebSocket server using a client (e.g., browser WebSocket API) at ws://localhost:9001.

Competitor Comparisons

21,534

Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js

Pros of ws

  • Pure JavaScript implementation, making it easy to understand and modify
  • Lightweight and minimal, with fewer dependencies
  • Widely adopted and battle-tested in production environments

Cons of ws

  • Generally slower performance compared to uWebSockets.js
  • Lacks some advanced features like SSL/TLS support out of the box
  • May consume more memory for large-scale applications

Code Comparison

ws:

const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });
});

uWebSockets.js:

const uWS = require('uWebSockets.js');

const app = uWS.App().ws('/*', {
  message: (ws, message, isBinary) => {
    console.log('received:', message);
  }
}).listen(8080, (listenSocket) => {
  if (listenSocket) {
    console.log('Listening to port 8080');
  }
});

The code comparison shows that uWebSockets.js has a more compact syntax for setting up a WebSocket server, while ws follows a more traditional Node.js event-based approach. uWebSockets.js also provides built-in routing capabilities, which can be beneficial for more complex applications.

60,879

Realtime application framework (Node.JS server)

Pros of Socket.IO

  • Easier to use and more beginner-friendly
  • Built-in support for multiple transport protocols (WebSocket, polling)
  • Extensive documentation and large community support

Cons of Socket.IO

  • Generally slower performance compared to uWebSockets.js
  • Higher memory usage and overhead
  • Less suitable for high-performance, low-latency applications

Code Comparison

Socket.IO server setup:

const io = require('socket.io')(3000);
io.on('connection', (socket) => {
  console.log('A user connected');
});

uWebSockets.js server setup:

const uWS = require('uWebSockets.js');
const app = uWS.App().ws('/*', {
  open: (ws) => {
    console.log('A user connected');
  }
}).listen(3000, (listenSocket) => {
  if (listenSocket) {
    console.log('Listening to port 3000');
  }
});

Summary

Socket.IO is more user-friendly and versatile, with better documentation and community support. However, uWebSockets.js offers superior performance and is better suited for high-performance applications. The choice between the two depends on the specific requirements of your project, balancing ease of use against performance needs.

A WebSocket Implementation for Node.JS (Draft -08 through the final RFC 6455)

Pros of WebSocket-Node

  • Pure JavaScript implementation, making it easier to understand and modify
  • Extensive documentation and examples for easier integration
  • Supports both server and client-side WebSocket implementations

Cons of WebSocket-Node

  • Generally slower performance compared to uWebSockets.js
  • Less efficient memory usage, especially for high-concurrency scenarios
  • May require additional dependencies for advanced features

Code Comparison

WebSocket-Node:

const WebSocketServer = require('websocket').server;
const server = new WebSocketServer({
  httpServer: httpServer,
  autoAcceptConnections: false
});

uWebSockets.js:

const uWS = require('uWebSockets.js');
const app = uWS.App().ws('/*', {
  open: (ws) => {
    console.log('A WebSocket connected!');
  }
});

WebSocket-Node provides a more traditional Node.js-style API, while uWebSockets.js offers a more streamlined and performance-oriented approach. uWebSockets.js is generally preferred for high-performance applications, while WebSocket-Node may be more suitable for projects prioritizing ease of use and pure JavaScript implementation.

4,472

:zap: Primus, the creator god of the transformers & an abstraction layer for real-time to prevent module lock-in.

Pros of Primus

  • Abstraction layer supporting multiple real-time frameworks
  • Extensive plugin ecosystem for added functionality
  • Easier to switch between different underlying transports

Cons of Primus

  • Higher-level abstraction may introduce overhead
  • Potentially slower performance compared to low-level solutions
  • Less control over fine-grained optimizations

Code Comparison

Primus:

const Primus = require('primus');
const server = require('http').createServer();
const primus = new Primus(server, { transformer: 'websockets' });

primus.on('connection', (spark) => {
  spark.write('Hello World!');
});

uWebSockets.js:

const uWS = require('uWebSockets.js');

const app = uWS.App().ws('/*', {
  open: (ws) => {
    ws.send('Hello World!');
  }
}).listen(9001, (listenSocket) => {
  if (listenSocket) {
    console.log('Listening to port 9001');
  }
});

Key Differences

  • Primus offers a higher-level API with more abstraction
  • uWebSockets.js provides a lower-level, performance-focused approach
  • Primus supports multiple transports, while uWebSockets.js is WebSocket-specific
  • uWebSockets.js likely offers better raw performance for WebSocket connections
  • Primus provides more built-in features and plugin support out-of-the-box
22,033

Package gorilla/websocket is a fast, well-tested and widely used WebSocket implementation for Go.

Pros of gorilla/websocket

  • Written in Go, offering excellent concurrency and performance
  • Simple and straightforward API, easy to use for Go developers
  • Well-documented and widely adopted in the Go ecosystem

Cons of gorilla/websocket

  • Limited to Go applications, not as versatile as uWebSockets.js
  • May have slightly higher memory usage compared to uWebSockets.js
  • Lacks some advanced features found in uWebSockets.js, such as pub/sub

Code Comparison

gorilla/websocket:

conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
    log.Println(err)
    return
}
defer conn.Close()

uWebSockets.js:

app.ws('/*', {
  open: (ws) => {
    console.log('A WebSocket connected!');
  },
  message: (ws, message) => {
    ws.send(message);
  }
});

Both libraries provide straightforward ways to handle WebSocket connections, but uWebSockets.js offers a more event-driven approach with its API. gorilla/websocket requires manual connection handling, while uWebSockets.js abstracts this process, making it easier to set up WebSocket servers quickly.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README


Simple, secure1 & standards compliant2 web server for the most demanding3 of applications. Read more...


:zap: Simple performance

µWebSockets.js is a standards compliant web server written in 10,000 lines of C++. It is exposed to Node.js as a simple-to-use, native V8 addon and performs at least 10x that of Socket.IO, 8.5x that of Fastify. It makes up the core components of Bun and is the fastest standards compliant web server in the TechEmpower (not endorsed) benchmarks.

We aren't in the NPM registry but you can easily install it with the NPM client:

  • npm install uNetworking/uWebSockets.js#v20.48.0
  • Browse the documentation and see the main repo. There are tons of examples but here's the gist of it all:

:dart: In essence

Untitled

:handshake: Permissively licensed

Intellectual property, all rights reserved.

Where such explicit notice is given, source code is licensed Apache License 2.0 which is a permissive OSI-approved license with very few limitations. Modified "forks" should be of nothing but licensed source code, and be made available under another product name. If you're uncertain about any of this, please ask before assuming.