Socket
Book a DemoInstallSign in
Socket

socket-udp

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

socket-udp

Basic UDP Socket and Client

latest
Source
npmnpm
Version
0.4.5
Version published
Maintainers
1
Created
Source

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)

Keywords

udp socket

FAQs

Package last updated on 15 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts