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

dgramx

v1.0.0

Published

An enhanced Node.js dgram module.

Downloads

15

Readme

dgramx

An enhanced Node.js dgram module.

This module extends the functionality of the internal dgram. By default, the on() and emit() method in dgram module acts just same as EventEmitter does, but this module change the way of how they bind a listener and emit an event.

This module allows you bind custom events, and instead of emitting them by the current object, the event will be emitted by the remote peer, just like how socket.io does, and build communication in a more easier way.

Example

const { createServer, createClient } = require("dgramx");

let addr = "udp://127.0.0.1:12345";
var server = createServer(addr);
var client = createClient(addr);

server.on("listening", () => {
    let { address, port } = server.address();
    console.log("Server listening at: %s:%d", address, port);
}).on("greet", (msg, rinfo) => {
    console.log(msg); // => Hello, World!

    // reply
    server.to(rinfo.address, rinfo.port).emit("reply", "Message recieved.");
});

client.bind(0); // the client binds a random port and wait for reply.
client.on("reply", (msg) => {
    console.log(msg); // => Message recieved.

    client.close();
    server.close();
}).emit("greet", "Hello, World!");

API

Socket

This class extends dgram.Socket, provides an easier way to communicate between two remote peers.

Socket.ReservedEvents: string[]

Sets what events are reserved by the module (or your module) and should be avoid using for remote emitting. By default, these events is reserved:

  • close triggered when the socket is closed.
  • error triggered when any error occurred.
  • listening triggered when bind() method is called.
  • message triggered when receiving any message.

Socket.prototype.on(event: string, listener: Function): Socket

If the event is reserved, then it acts just as talked above. But if the event is customized, then the listener function will be called when the remote peer emits the event. The signature of the listener function is:

(msg: any, rinfo: dgram.RemoteInfo) => void

  • msg The message needed to be sent.
  • rinfo Remote information contains:
    • address: string the address of the remote peer.
    • port: number binding port of the remote peer.
    • family: "IPv4" | "IPv6"

Socket.prototype.emit(event: string, msg: any): boolean

Apart from reserved events, you can emit any customized event to the remote peer, and sends data when the event fires. Be aware, the number of data should be equal to the one specified by the remote peer listener.

Socket.prototype.to()

Sets the receiver information of the remote peer, valid signatures are:

  • to(addr: string) e.g. to("localhost:12345").
  • to(port: number) e.g. to(1234).
  • to(addr: string, port: number) e.g to("localhost", 12345)

You can call this method several times to set multiple receivers, but after emit() is called, the receivers will be set to empty.

createSocket()

The same as dgram.createSocket(), only it returns the Socket created by this module.

createServer(addr: string, callback?: () => void)

Creates a UDP server according to the given address. This is a short-hand for createSocket(type).bind(port, addr).

The addr must contain a protocol, could be either udp, udp4, udp6.

createClient(addr: string)

Creates a UDP client ready to the given server. This is a short-hand for createSocket(type).to(addr, port).

The addr of createClient() is the same and should be the same as the one of createServer().

The Data Frame

For efficiency concerns, this module uses JSON to encode and decode data into and from buffer.