VikramAditya33/uWestJS
High-performance WebSocket adapter for NestJS using μWebSockets.js
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:
uWestJS brings this performance to NestJS without requiring you to change your existing code.
No download data available
No tracked packages depend on this.
@SubscribeMessage, @MessageBody, @ConnectedSocket decorators → Decoratorsnpm install uwestjs
Or using yarn:
yarn add uwestjs
Or using pnpm:
pnpm add uwestjs
npm cache clean --force before installingimport { 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 { SHARED_COMPRESSOR } from 'uwestjs';
const adapter = new UwsAdapter(app, {
port: 8099,
path: '/ws',
maxPayloadLength: 16384,
idleTimeout: 60,
compression: SHARED_COMPRESSOR,
cors: {
origin: 'https://example.com',
credentials: true,
},
});
See Adapter Documentation for all configuration options.
Both HTTP and WebSocket support flexible CORS configuration:
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, // Auto-wrapped internally
});
See HTTP Middleware and WebSocket Middleware for usage patterns.
@SubscribeMessage, @MessageBody, @ConnectedSocketKey differences when migrating from Express:
UwsPlatformAdapter instead of Express adapterapp.init() then adapter.listen() instead of app.listen()See Server Documentation for detailed migration guide.
Key differences when migrating from Socket.IO adapter:
UwsAdapter instead of IoAdapteradapter.registerGateway(gateway)See Adapter Documentation for detailed migration guide.
uWestJS provides significant performance improvements:
For performance tips and benchmarks, see Server Documentation.
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)# 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:watch
uWestJS delivers exceptional performance compared to traditional Node.js frameworks:
| Scenario | Express req/s | Fastify req/s | uWestJS req/s | Express throughput | Fastify throughput | uWestJS throughput | uWestJS vs Express | uWestJS vs Fastify |
|---|---|---|---|---|---|---|---|---|
| compression | 21.47k | 11.75k | 24.40k | 11.28 MB/s | 6.02 MB/s | 11.22 MB/s | 1.14x | 2.08x |
| headers | 51.45k | 83.36k | 116.87k | 9.57 MB/s | 15.58 MB/s | 18.39 MB/s | 2.27x | 1.40x |
| hello-world | 54.97k | 85.83k | 138.49k | 9.12 MB/s | 14.41 MB/s | 13.74 MB/s | 2.52x | 1.61x |
| json-response | 51.11k | 79.57k | 113.99k | 13.94 MB/s | 21.78 MB/s | 27.83 MB/s | 2.23x | 1.43x |
| mixed-response | 51.62k | 79.64k | 112.89k | 11.91 MB/s | 18.46 MB/s | 22.82 MB/s | 2.19x | 1.42x |
| post-json | 45.23k | 39.57k | 79.88k | 8.37 MB/s | 10.79 MB/s | 13.56 MB/s | 1.77x | 2.02x |
| query-params | 41.64k | 79.31k | 121.08k | 9.17 MB/s | 17.55 MB/s | 16.28 MB/s | 2.91x | 1.53x |
| route-params | 49.30k | 78.81k | 116.46k | 11.24 MB/s | 18.04 MB/s | 23.21 MB/s | 2.36x | 1.48x |
| static-file | 57.16k | 70.95k | 107.60k | 567.34 MB/s | 703.25 MB/s | 1.04 GB/s | 1.88x | 1.52x |
| streaming-upload | 324.32 | 324.09 | 338.69 | 72.21 KB/s | 67.73 KB/s | 65.49 KB/s | 1.04x | 1.05x |
Test Environment:
Run benchmarks yourself:
# Quick benchmark (10s per scenario)
npm run benchmark:quick
# Full benchmark (20s per scenario, saves results)
npm run benchmark
# Test benchmark setup
npm run benchmark:test
Or from the benchmarks directory:
cd benchmarks
npm install
npm run benchmark:quick
See benchmarks/README.md for detailed information about metrics collected, historical tracking, and CI/CD integration.
This project is licensed under the MIT License - see the LICENSE file for details.
Vikram Aditya
Part of FOSS FORGE