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 🙏

© 2026 – Pkg Stats / Ryan Hefner

nestjs-udp

v1.0.2

Published

A lightweight UDP communication module for NestJS with routing, decorators, and dynamic client support.

Readme

npm version license

English | 🇨🇳 中文文档

NestJS UDP Communication Module — Seamlessly integrate UDP protocol into your Nest application with routing, reactive response handling, and dynamic targeting.

nestjs-udp

A high-performance UDP communication module based on the NestJS microservices framework. It offers a decorator-driven, modular, and client-friendly experience similar to @nestjs/microservices. Ideal for embedded systems, LAN communication, edge gateways, and other lightweight UDP scenarios.


✨ Features

  • 🚀 Define handlers using Nest-style @UdpPattern() decorators
  • 📦 Built-in UdpClientProxy supporting both sync and async messaging
  • 🧩 Fully modular — import via UdpModule.register()
  • 🧠 Pattern-based routing, consistent with HTTP/RPC style
  • 🔧 Configurable host, port, multicast, and socket type
  • 📈 Built-in sequence diagrams to illustrate common use cases

⚡ Quick Start

Installation

# With npm / yarn / pnpm
npm install nestjs-udp
# OR
yarn add nestjs-udp
# OR
pnpm add nestjs-udp

📦 Exports Overview

  • UdpClientProxy: UDP client implementation
  • UdpServer: UDP server strategy
  • UdpModule: Registerable NestJS module
  • UdpPattern(): Request pattern decorator
  • UDP_CLIENT: Injection token for the client

Usage

  1. Register and start the UDP server
import { UdpServer } from "nestjs-udp";

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.connectMicroservice({
    strategy: new UdpServer({ port: 34567, host: "0.0.0.0" }),
  });

  await app.startAllMicroservices();
  await app.listen(3000);
}
bootstrap();
  1. Define handlers with decorators
import { Controller, Get } from "@nestjs/common";
import { UdpPattern, UDP_CLIENT, UdpContext } from "nestjs-udp";

@Controller()
export class AppController {
  constructor(
    // Inject UdpClientProxy client for sending UDP messages
    @Inject(UDP_CLIENT) private readonly udpClient: ClientProxy
  ) {}
  // Define a handler for pattern "UDP:ping"
  // @Payload() data: message payload
  // @Ctx() ctx: UdpContext instance with source address information
  // Return value will be sent as UDP response
  @UdpPattern("UDP:ping")
  ping(data: any) {
    return {
      data,
    };
  }

  // No return value means no response will be sent
  @MessagePattern("UDP:noResponse")
  pingResponse(@Payload() data: any, @Ctx() ctx: UdpContext) {
    console.log("no response", data);
  }

  @Get("await")
  async pingUdp() {
    // this.udpClient.send returns an Observable
    //  You can use RxJS operators to handle the response
    // Synchronously wait for UDP response from local server
    const res = await firstValueFrom(
      this.udpClient.send({ cmd: "UDP:ping" }, "hello world")
    );
    return res;
  }

  @Get("async")
  async pingAsync() {
    // Send UDP message without waiting for a response (fire-and-forget)
    // Dynamically specify destination address
    this.udpClient
      .send({ cmd: "UDP:ping", host: "127.0.0.1", port: 43210 }, "balabala")
      .subscribe();

    return "Sent successfully";
  }

  @Get("async-awit")
  async pingAsyncAwait() {
    // Send UDP message and handle response asynchronously
    // Dynamically specify destination address
    this.udpClient.send({ cmd: "UDP:ping" }, "balabala").subscribe({
      next: (res) => {
        console.log("response", res);
      },
      error: (err) => {
        console.log("error", err);
      },
      complete: () => {
        console.log("completed");
      },
    });

    return "Sent successfully";
  }
}

Message Structure

Outgoing Message

interface UdpPacket {
  pattern: string | { cmd: string; host?: string; port?: number };
  data: any;
}
  • pattern: Can be a simple string (e.g., "UDP:ping") or an object with dynamic target (host, port)
  • data: The payload being sent

Response Structure

All responses are JSON-encoded. The data field contains the application-level result.

Transmission Format

All UDP messages are serialized to JSON strings:

The fields pattern.host and pattern.port are optional and are used to dynamically specify the target address.

  1. Static target:
{
  "pattern": "UDP:ping",
  "data": {
    "payload": "..."
  }
}
  1. Dynamic target:
{
  "pattern": { "cmd": "UDP:ping", "host": "127.0.0.1", "port": 43210 },
  "data": "some payload"
}

📊 Sequence Diagrams

Fire-and-forget UDP (no response expected)

sequenceDiagram
    participant User as 🌐 HTTP Client
    participant Controller as HTTP Controller
    participant UdpClient as UdpClientProxy
    participant UdpServer as UDP Server
    participant Business as UDP Handler

    User->>Controller: GET /api/router
    Controller-->>User: Immediate HTTP response
    Controller->>UdpClient: udpClient.send({ pattern, host, port }, data)
    UdpClient->>UdpServer: Send UDP packet
    UdpServer->>Business: Dispatch based on pattern

Awaiting UDP Response

sequenceDiagram
    participant C as 🌐 HTTP Client
    participant HC as HTTP Controller
    participant UCP as UDP ClientProxy
    participant US as UDP Server
    participant UH as UDP Handler

    C->>HC: Send HTTP request
    HC->>UCP: await firstValueFrom(udpClient.send(pattern, data))
    UCP->>US: Send UDP packet
    US->>UH: Dispatch to handler
    UH-->>US: Return response
    US->>UCP: Send back response
    UCP-->>HC: Receive and return result
    HC-->>C: HTTP response (contains UDP data)

HTTP Response Immediately, UDP Processed Later

sequenceDiagram
    participant C as 🌐 HTTP Client
    participant HC as HTTP Controller
    participant UCP as UDP ClientProxy
    participant US as UDP Server
    participant UH as UDP Handler


    C->>HC: Send HTTP request
    HC->>UCP: udpClient.send(pattern, data).subscribe({next: (res) => { callback function ... }})
    HC-->>C: Immediate HTTP response

    UCP->>US: Send UDP packet
    US->>UH: Dispatch to handler
    UH-->>US: Return response
    US->>UCP: Send back response
    UCP-->>HC: Handle via callback (e.g. log, DB insert)

📌 TODOs & Roadmap

  • [ ] Add optional message encryption and authentication
  • [ ] Integrate heartbeat and retry mechanisms
  • [ ] Add message queue for persistence support

❤️ Acknowledgements

Inspired by the @nestjs/microservices module. Thanks to the NestJS team for their fantastic architecture and open-source contribution.