npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@thanhhoajs/websocket

v1.0.9

Published

A powerful WebSocket library built on top of Bun's native WebSocket implementation

Downloads

23

Readme

@thanhhoajs/websocket

A high-performance, feature-rich WebSocket library supercharging Bun's native WebSocket implementation. Designed for developers who demand speed, flexibility, and ease of use in their real-time applications.

@thanhhoajs/websocket seamlessly blends the raw power of Bun's WebSocket with an intuitive API, bringing you the best of both worlds. Whether you're building a chat application, a live dashboard, or a complex real-time system, this library provides the tools you need to create robust, scalable WebSocket servers with minimal effort.

Key highlights:

  • 🚀 Lightning-fast performance leveraging Bun's speed
  • 🛠 Intuitive routing and middleware system
  • 📡 Built-in pub/sub functionality for effortless broadcasting
  • 🔒 TypeScript support for type-safe development
  • 🎛 Fine-grained control over WebSocket lifecycle events
  • 🧩 Easily extensible for custom use cases

Embrace the future of real-time web applications with @thanhhoajs/websocket – where performance meets developer productivity.

Features

  • High Performance: Built on Bun's native WebSocket implementation for optimal speed and efficiency
  • Easy Setup: Simple API to quickly create and configure WebSocket servers
  • Routing System: Flexible routing mechanism to handle different WebSocket endpoints
  • Middleware Support: Global and route-specific middleware for customizable request/message processing
  • Event-Driven Architecture: Built-in event system for easy handling of WebSocket lifecycle events
  • Grouping Routes: Ability to group routes with common prefixes for better organization
  • Pub/Sub Functionality: Built-in publish/subscribe system for efficient message broadcasting
  • Type Safety: Full TypeScript support for enhanced developer experience and code reliability
  • Custom Data Handling: Support for attaching custom data to WebSocket connections
  • Flexible Message Types: Handle string, Buffer, and various ArrayBuffer types for messages
  • Header Validation: Option to validate headers before upgrading to WebSocket connection
  • Query and Param Parsing: Automatic parsing of URL query parameters and route params
  • Broadcast Capability: Easily send messages to all connected clients
  • Topic-Based Communication: Subscribe clients to specific topics and publish messages to those topics
  • Connection Management: Methods to handle WebSocket connections, including subscription status checks
  • Graceful Shutdown: Ability to stop the WebSocket server cleanly
  • Server Statistics: Access to server stats like pending connections and route count
  • Compression Support: Option to compress messages for reduced bandwidth usage
  • Customizable Serve Options: Flexible configuration options inherited from Bun's WebSocket serve options
  • Error Handling: Built-in error handling and event emission for various WebSocket scenarios
  • Extensibility: Easy to extend and integrate with other parts of your application
  • Low Memory Footprint: Efficient memory usage, ideal for high-concurrency scenarios
  • Cross-Platform: Works on any platform supported by Bun
  • Open Source: MIT licensed for use in both personal and commercial projects

Installation

Make sure you have Bun installed (version 1.0.0 or later). Then, you can install the package using:

bun add @thanhhoajs/websocket

Quick Start

Here's a simple example of how to use ThanhHoaWebSocket:

import { ThanhHoaWebSocket, RouterHandler } from '@thanhhoajs/websocket';

const ws = new ThanhHoaWebSocket({ port: 3000 });
const router = new RouterHandler();

router.route('chat', {
  onOpen: (ws, query, params) => {
    console.log('New connection');
    ws.send('Welcome to the chat!');
  },
  onMessage: (ws, message) => {
    console.log(`Received: ${message}`);
    ws.send(`You said: ${message}`);
  },
  onClose: (ws, code, reason) => {
    console.log(`Connection closed: ${code} - ${reason}`);
  },
});

ws.group('', router);

// Logger
ws.logger();

console.log(`WebSocket server is running on ws://localhost:${ws.port}`);

Advanced Usage

Here's an example showcasing more advanced features of ThanhHoaWebSocket, including middleware, grouping, pub/sub functionality, and custom data handling:

import {
  ThanhHoaWebSocket,
  RouterHandler,
  type WebSocketMiddleware,
  type IThanhHoaWebSocketData,
} from '@thanhhoajs/websocket';
import type { ServerWebSocket } from 'bun';

// Create a new WebSocket server
const ws = new ThanhHoaWebSocket({ port: 3000 });
const router = new RouterHandler();

// Define a middleware
const authMiddleware: WebSocketMiddleware = async (
  ws: ServerWebSocket<IThanhHoaWebSocketData>,
) => {
  const token = ws.data.headers.get('Authorization');
  if (!token) {
    ws.close(1008, 'Unauthorized');
    return false;
  }
  // Perform token validation here
  ws.data.custom = { userId: 'user123' }; // Attach custom data
  return true;
};

// Define route handlers
const chatHandler = {
  onOpen: (
    ws: ServerWebSocket<IThanhHoaWebSocketData>,
    query?: Record<string, string>,
  ) => {
    console.log(`New chat connection. User ID: ${ws.data.custom?.userId}`);
    ws.subscribe('general'); // Subscribe to 'general' topic
    ws.send('Welcome to the chat!');
  },
  onMessage: (
    ws: ServerWebSocket<IThanhHoaWebSocketData>,
    message: string | Buffer,
  ) => {
    console.log(`Received: ${message}`);
    ws.publish('general', `User ${ws.data.custom?.userId} says: ${message}`);
  },
  onClose: (
    ws: ServerWebSocket<IThanhHoaWebSocketData>,
    code: number,
    reason: string,
  ) => {
    console.log(`Chat connection closed: ${code} - ${reason}`);
  },
};

// Add routes
router.route('chat', authMiddleware, chatHandler);

// Group routes
ws.group('api', authMiddleware, router);

// Global error handling
ws.on('error', (error, ws) => {
  console.error('WebSocket error:', error);
  ws.close(1011, 'Internal Server Error');
});

// Broadcast server status every 5 seconds
setInterval(() => {
  const stats = ws.getStats();
  ws.broadcast(JSON.stringify({ type: 'serverStatus', data: stats }));
}, 5000);

console.log(
  `Advanced WebSocket server is running on ws://localhost:${ws.port}`,
);

This advanced example demonstrates:

  1. Using middleware for authentication
  2. Custom header validation
  3. Attaching and using custom data on WebSocket connections
  4. Implementing pub/sub with topics
  5. Grouping routes with a common prefix
  6. Global error handling
  7. Broadcasting server statistics to all clients
  8. Using the event emitter for custom events

API Overview

ThanhHoaWebSocket

The main class for creating and managing a WebSocket server.

  • constructor(options: ThanhHoaWebSocketOptions): Creates a new WebSocket server
  • use(middleware: WebSocketMiddleware): Adds a global middleware
  • group(prefix: string, ...args: (WebSocketMiddleware | RouterHandler)[]): Groups routes with a common prefix
  • broadcast(message: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer, compress?: boolean): Sends a message to all connected clients
  • subscribe(ws: ServerWebSocket, topic: string): Subscribes a client to a topic
  • publish(ws: ServerWebSocket, topic: string, message: string | Bun.BufferSource, compress?: boolean): Publishes a message to a topic

RouterHandler

Manages WebSocket routes.

  • route(path: string, ...args: (WebSocketMiddleware | IWebSocketRouteHandler)[]): Adds a new route

Performance

ThanhHoaWebSocket is built for high performance. In our benchmarks:

Time to connect 16 WebSockets: 9.73ms
Time to send and receive 1,000,000 messages: 5990.51ms

Benchmark Details

  • Connections: 16 WebSocket connections
  • Messages: 62500 messages per connection (1,000,000 total)
  • Environment: [R7 8845H, 32GB 7500MHz, AMD Radeon 780M]

Interpretation

  • The library can establish 16 WebSocket connections in just 9.73ms, demonstrating fast connection handling.
  • It can process 1,000,000 messages (send and receive) in 5990.51ms, showing high throughput capabilities.

Author

Nguyen Nhu Khanh [email protected]

License

MIT License