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

socket-udp

v0.4.5

Published

Basic UDP Socket and Client

Downloads

266

Readme

UDP Socket

npm tests CodeQL CodeFactor Grade JavaScript Style Guide node-current GitHub

Plain UDP Socket and Client

  • Fast — little overhead above UDP to send messages
  • Simple — used well-known Node streams to manipulate and move data
  • Zero-dependency
  • ESM and CJS

Install

npm i --save socket-udp

Fast Start

//app.js
import { UDPClient } from 'socket-udp'

const client = new UDPClient({ port: 44002 })

client.write(Buffer.from('Hello, World!', 'utf8'))
//server.js
import { UDPSocket } from 'socket-udp'

const socket = new UDPSocket({ port: 44002 })

for await (const message of socket) {
  console.log(message.toString('utf8'))
}

After just start the server node server.js and start your app node app.js. That's all, you've just sent and received message.

Documentation

class UDPClient

Extends Writabable Stream

Arguments:

Extends WritableOptions and dgram.SocketOptions

  • options <object> – optional
    • type <'udp4' | 'udp6'> – optional. Default 'udp4'
    • port <string | number> – optional. Target port. Default 44002
    • address <string> – optional. Target address. Default '127.0.0.1' or '::1'
    • bindAddress <dgram.BindOptions> – optional.
      • port <integer> — optional.
      • address <string> — optional.
      • exclusive <boolean> — optional.
      • fd <integer> — optional.

Fields:

  • origin: <dgram.Socket>
  • port: <number>
  • address: <string>
  • family: <string>
  • allowWrite: <boolean>
  • targetPort: <number>
  • targetAddress: <number>

Events:

Event: 'ready'

Emitted when the client "establishes" udp connection.

Usage

Simple example
import { UDPClient } from 'socket-udp'

const client = new UDPClient({ port: 44002 })

client.write(Buffer.from('hi!', 'utf8'))

class UDPSocket

Extends Readable Stream

It is a UDP socket in readable stream form.

Arguments:

Extends ReadableOptions and dgram.SocketOptions

  • options <object>required
    • type <'udp4' | 'udp6'> – optional. Default 'udp4'
    • port <string | number> – optional. Default 44002
    • address <string> – optional. Default '127.0.0.1' or '::1'
    • pushMeta <boolean> – optional. Will push not a raw message, but an object with remoteInfo. Message data will be placed in field body. Default false

Fields:

  • origin: <dgram.Socket>
  • port: <number>
  • address: <string>
  • family: <string>
  • allowPush: <boolean>

Events:

All Readable events of course and:

Event: 'ready'

Emitted when socket started and ready to receive data.

Event: 'data'

Emitted right after a message was received

  • message <Buffer>

Methods:

  • handleMessage (body: Buffer, head: MessageHead) => void – handles raw messages from dgram.Socket. If you need to handle data before any manipulation then overwrite it.

Usage

Example how to use socket as stream
import fs from 'node:fs'
import { UDPSocket } from 'socket-udp'

const socket = new UDPSocket()
const writer = fs.createWriteStream('/some/path')

socket.pipe(writer)
Example how to use plain socket as async generator + pushMeta example
import { UDPSocket } from 'socket-udp'

const socket = new UDPSocket({ port: 44002, pushMeta: true })

for await (const { address, port, body } of socket) {
  console.log(`From ${address}:${port} you recieved`, JSON.parse(body.toString('utf8')))
}

Additional Exposed variables and functions

constant DEFAULT_PORT

  • <number> : 44002

License (MIT)