High-performance HTTP and WebSocket platform adapter for NestJS using uWebSockets.js
uWestJS is a high-performance platform adapter for NestJS, powered by uWebSockets.js. It provides both HTTP and WebSocket capabilities with significantly better performance while maintaining full compatibility with NestJS decorators and patterns you already know.
uWebSockets.js is one of the fastest HTTP and WebSocket implementations available, offering:
- Up to 10x faster than Express and traditional WebSocket libraries in benchmarks
- Lower memory footprint for handling thousands of concurrent connections
- Native backpressure handling to prevent memory issues under load
- Built-in compression support for reduced bandwidth usage
- Streaming support with automatic backpressure management
uWestJS brings this performance to NestJS without requiring you to change your existing code.
- Adapter - UwsAdapter configuration and methods
- Socket API - UwsSocket properties and methods
- Broadcasting - BroadcastOperator and broadcasting patterns
- Decorators - NestJS WebSocket decorators
- Rooms - Room operations and patterns
- Middleware - Guards, Pipes, and Filters
- Exceptions - WsException and error handling
- Lifecycle - Gateway lifecycle management
- Server - Server setup and configuration
- Request - HTTP request object and methods
- Response - HTTP response object and methods
- Routing - Route registration and path parameters
- Middleware - Guards, Pipes, Filters, and Interceptors
- Body Parsing - JSON, form data, and more
- Multipart - File uploads and multipart forms
- Compression - Request/response compression
- CORS - Cross-origin resource sharing
- Static Files - Static file serving
- High Performance - Up to 10x faster than Express for HTTP requests → Server Documentation
- Request Handling - Full Express-compatible request API with body parsing, headers, cookies, and more → Request API
- Response Handling - Comprehensive response methods including streaming, compression, and caching → Response API
- Routing - Path parameters, wildcards, and route registration → Routing Guide
- Body Parsing - Automatic parsing for JSON, URL-encoded, multipart, raw, and text → Body Parsing
- File Uploads - Multipart form data and file upload handling → Multipart Guide
- Static Files - Advanced static file serving with caching, range requests, and ETag support → Static Files
- Middleware - Full support for Guards, Pipes, Filters, and Interceptors → HTTP Middleware
- Compression - Request and response compression support → Compression
- CORS - Flexible cross-origin resource sharing configuration → CORS Configuration
- NestJS Compatibility - Full support for
@SubscribeMessage,@MessageBody,@ConnectedSocketdecorators → Decorators - Room Management - Efficient room-based broadcasting and client organization → Rooms Guide
- Broadcasting - Powerful broadcasting operators for targeted message distribution → Broadcasting
- Middleware Support - Guards, Pipes, and Filters for WebSocket handlers → WebSocket Middleware
- Lifecycle Management - Gateway lifecycle hooks and connection management → Lifecycle
- Exception Handling - WsException and error handling patterns → Exceptions
- Backpressure Handling - Automatic message queuing when clients are slow
- CORS Configuration - Built-in CORS support for WebSocket connections
- Compression - Per-message deflate compression support
- TypeScript Support - Full type definitions and TypeScript-first design
- Dependency Injection - Full NestJS DI support for all middleware
- Shared or Separate Ports - Run HTTP and WebSocket on the same port or separate ports
- Production Ready - Comprehensive test coverage and battle-tested in production
npm install uwestjsOr using yarn:
yarn add uwestjsOr using pnpm:
pnpm add uwestjs- Node.js 20, 22, 24, or 25
- NestJS >= 11.0.0
- TypeScript >= 6.0.0
- Supported Node.js versions: 20, 22, 24, 25
- If you experience installation or runtime issues, run
npm cache clean --forcebefore installing
import { NestFactory } from '@nestjs/core';
import { UwsPlatformAdapter } from 'uwestjs';
import { AppModule } from './app.module';
async function bootstrap() {
const adapter = new UwsPlatformAdapter();
const app = await NestFactory.create(AppModule, adapter);
await app.init();
adapter.listen(3000, () => {
console.log('HTTP server running on port 3000');
});
}
bootstrap();See Server Documentation for detailed setup instructions.
import { NestFactory } from '@nestjs/core';
import { UwsAdapter } from 'uwestjs';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const adapter = new UwsAdapter(app, { port: 8099 });
app.useWebSocketAdapter(adapter);
// Replace YourGateway with your actual gateway class
const gateway = app.get(YourGateway);
adapter.registerGateway(gateway);
await app.listen(3000);
}
bootstrap();See Adapter Documentation for detailed setup instructions.
import { NestFactory } from '@nestjs/core';
import { UwsPlatformAdapter, UwsAdapter } from 'uwestjs';
import { AppModule } from './app.module';
async function bootstrap() {
const httpAdapter = new UwsPlatformAdapter();
const app = await NestFactory.create(AppModule, httpAdapter);
const wsAdapter = new UwsAdapter(app, {
uwsApp: httpAdapter.getHttpServer(),
path: '/ws'
});
app.useWebSocketAdapter(wsAdapter);
// Replace YourGateway with your actual gateway class
const gateway = app.get(YourGateway);
wsAdapter.registerGateway(gateway);
await app.init();
httpAdapter.listen(3000, () => {
console.log('HTTP and WebSocket running on port 3000');
});
}
bootstrap();See Server Documentation for more deployment patterns.
Configure the HTTP platform adapter with SSL, compression, and more:
const adapter = new UwsPlatformAdapter({
key_file_name: 'key.pem',
cert_file_name: 'cert.pem',
});See Server Documentation for all configuration options.
Configure the WebSocket adapter with compression, timeouts, CORS, and more:
import * as uWS from 'uWebSockets.js';
const adapter = new UwsAdapter(app, {
port: 8099,
path: '/ws',
maxPayloadLength: 16384,
idleTimeout: 60,
compression: uWS.SHARED_COMPRESSOR,
cors: {
origin: 'https://example.com',
credentials: true,
},
});See Adapter Documentation for all configuration options.
Both HTTP and WebSocket support flexible CORS configuration:
- Specific origins (recommended for production)
- Multiple origins
- Dynamic origin validation
- Credentials support
See HTTP CORS and Adapter Documentation for details.
Enable dependency injection for Guards, Pipes, and Filters:
import { ModuleRef } from '@nestjs/core';
const moduleRef = app.get(ModuleRef);
const adapter = new UwsAdapter(app, {
port: 8099,
moduleRef,
});See HTTP Middleware and WebSocket Middleware for usage patterns.
- Request Handling - Access headers, query params, body, cookies
- Response Methods - Send JSON, HTML, files, streams
- Routing - Define routes with path parameters
- Body Parsing - Parse JSON, form data, multipart
- File Uploads - Handle file uploads
- Static Files - Serve static assets
- Middleware - Use Guards, Pipes, Filters, Interceptors
- Compression - Enable compression
- CORS - Configure cross-origin requests
- Decorators - Use
@SubscribeMessage,@MessageBody,@ConnectedSocket - Rooms - Organize clients into rooms
- Broadcasting - Send messages to multiple clients
- Middleware - Use Guards, Pipes, Filters
- Lifecycle - Handle connection/disconnection events
- Exceptions - Handle errors gracefully
- UwsPlatformAdapter API - HTTP platform adapter methods
- UwsAdapter API - WebSocket adapter methods
- UwsSocket API - Socket instance methods
- BroadcastOperator API - Broadcasting methods
- Request API - HTTP request methods
- Response API - HTTP response methods
Key differences when migrating from Express:
- Use
UwsPlatformAdapterinstead of Express adapter - Initialize with
app.init()thenadapter.listen()instead ofapp.listen() - Most Express APIs work the same (req, res, middleware)
See Server Documentation for detailed migration guide.
Key differences when migrating from Socket.IO adapter:
- Use
UwsAdapterinstead ofIoAdapter - Register gateways with
adapter.registerGateway(gateway) - All NestJS decorators work the same way
See Adapter Documentation for detailed migration guide.
uWestJS provides significant performance improvements:
- HTTP: Up to 10x faster than Express
- WebSocket: Up to 10x faster than Socket.IO
- Memory: Lower memory footprint for concurrent connections
- Backpressure: Automatic handling prevents memory issues
- Compression: Built-in support reduces bandwidth usage
For performance tips and benchmarks, see Server Documentation.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
# Run all tests
npm test
# Run unit tests only
npm run test:unit
# Run integration tests only
npm run test:integration
# Run tests with coverage
npm run test:cov
# Run tests in watch mode
npm run test:watchThis project is licensed under the MIT License - see the LICENSE file for details.
- Built on top of uWebSockets.js
- Designed for NestJS
- Inspired by the NestJS community's need for high-performance WebSocket solutions
- GitHub Issues: Report a bug
- GitHub Discussions: Ask questions
Vikram Aditya
Part of FOSS FORGE - Open Source Tools & Libraries
