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

udpgroup

v0.1.0

Published

A utility for managing multiple udp destinations

Downloads

3

Readme

udpgroup

What is udpgroup?

udpgroup creates a simple API wrapper around the Node JS dgram module to organize multiple duplex functions on a single UDP port.

Simple Implementation:

Let's imagine that we have a setup like this:

  • On one machine, we're running MainNodeJSApp that needs to handle UDP packets to and from various sources, process them, and send them on to another service over HTTP.
  • On a separate Remote Device, we have two dedicated processes that also will send and receive UDP packets:
    • The Telemetry process is assigned to port 8001
    • The Files process is assigned to port 8005
  • MainNodeJSApp listens and sends to Remote Device on port 40004.
  • Internally, MainNodeJSApp is using NodeJS Streams to both handle files received and process telemetry data. Both of those data structures are completely different, so MainNodeJSApp has two different NodeJS streams to process them.

We can imagine our setup looks something like this:


 ┌─────────────────────────┐
 │                         │                                  ┌─────────────────┐
 │                       ┌─┴─────┐                            │                 │
 │    Telemetry Process  │ :8001 │◄────────────┐              │    Some Other   │
 │                       └─┬─────┘             │              │      Service    │
 │     ┌─────────────┐     │                   │              │                 │
 │     │Remote Device├─────┤                   │              │                 │
 ├─────┤192.175.12.24│     │                   │              └─────────────────┘
 │     └─────────────┘     │                   │                          ▲
 │                       ┌─┴─────┐             │                          │
 │      Files Process    │ :8005 │◄──────────┐ │                          │
 │                       └─┬─────┘           │ │                          │
 │                         │                 │ │                          │
 └─────────────────────────┘                 │ │                          │
                                             │ │                      ┌───┴──┐
                                             │ │              ┌───────┤ HTTP ├──┐
                                             │ │              │       └──────┘  │
                                             │ └───────►┌─────┴──┐              │
                                             │          │ :40004 │              │
                                             └─────────►└─────┬──┘              │
                                                              │ Main NodeJS App │
                                                              │                 │
                                                              │                 │
                                                              │                 │
                                                              └─────────────────┘

We'll use udpgroup to coordinate the messages to and from the Telemetry and Files processes on Remote Device.

First, we'll require the library:

const UdpGroup = require('udpgroup');

Then we'll instantiate a new UdpGroup with the port that we'll be listening on:

const udpg = new UdpGroup({ listen_port: 40004 });

We'll make sure that the streams we want to divert to have been created, then we'll use the .addPathway method to add a named pathway for files and telemetry. The .addPathway method takes a config object and a callback function.

const telemStream = createTelemetryStreamSomehow();
const filesStream = createFilesStreamSomehow();

udpg.addPathway(
  { remote_name: 'telemetryService', remote_ip: '192.175.12.24', remote_port: 8001 },
  (err, remoteStream) => {
    if (!err) {
      remoteStream.pipe(telemStream);
    }
  }
);

udpg.addPathway(
  { remote_name: 'filesService', remote_ip: '192.175.12.24', remote_port: 8005 },
  (err, remoteStream) => {
    if (!err) {
      remoteStream.pipe(filesStream);
    }
  }
);

As we can see above, the callback function will be called with an Error as its first argument if something goes wrong. If everything went fine, the first argument will be null. The second argument will be the NodeJS Stream object associated with this pathway. The third argument will be the string identifier for that pathway: either the passed remote_name value or a string generated by the UdpGroup that will look like ${remote_ip}_${remote_port} (or just ${remote_ip} if no remote port was associated with the pathway).

We can also use our UdpGroup to send messages from the Group's port to our services. For example, perhaps when the file service finishes receiving a file, it emits a confirmation code that should be sent back to the file service on the Remote Device.

filesStream.on('fileComplete', hashCode => {
  udpg.send(Buffer.from(hashCode), 'filesService');
});

Since our UdpGroup recognizes the string "fileService" as one of our defined pathways, it will send the hashCode Buffer over UDP to 192.175.12.24:8005.

TODO

  • Allow implementer to set default offset and length
  • Allow split sends for messages that overrun the length
    • Should there be an option of truncate message to send on length or send multiple messages if send buffer is longer than length?