0.4.5 • Published 1 year ago

socket-udp v0.4.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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)

0.4.5

1 year ago

0.4.4

1 year ago

0.4.3

1 year ago

0.4.2

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

1.0.0

1 year ago