LogoPear Docs

bare-dgram

Native UDP sockets for JavaScript

Documented against v1.1.1
stable

bare-dgram — Native UDP sockets for JavaScript. It is a native addon and requires Bare >=1.16.0.

Mirrors the Node.js dgram module.

npm i bare-dgram

Usage

const dgram = require('bare-dgram')

const client = dgram.createSocket('udp4')
const server = dgram.createSocket('udp4')

client.on('connect', () => client.send('hello'))

server.on('message', (msg, rinfo) => {
  console.log(`message: ${msg} from: ${rinfo.address}:${rinfo.port}`)
})

server.bind(() => client.connect(server.address().port))

API

DgramSocket

DgramSocket

new DgramSocket(opts?: DgramSocketOptions | DgramSocketType | null, onmessage?: (message: Buffer, remote: DgramRemoteInfo) => void)

Parameters

ParameterTypeDefaultDescription
opts?DgramSocketOptions | DgramSocketType | null
onmessage?(message: Buffer, remote: DgramRemoteInfo) => void

addMembership(group: string, iface?: string | null): void

Parameters

ParameterTypeDefaultDescription
groupstring
iface?string | null

address(): DgramSocketAddress | null

addSourceSpecificMembership(source: string, group: string, iface?: string | null): void

Parameters

ParameterTypeDefaultDescription
sourcestring
groupstring
iface?string | null

bind(port?: number, address?: string, onlistening?: () => void): this

Overloads:

bind(port?: number, address?: string, onlistening?: () => void): this
bind(port: number, onlistening: () => void): this
bind(opts: DgramSocketBindOptions, onlistening?: () => void): this
bind(onlistening: () => void): this

Parameters

ParameterTypeDefaultDescription
port?number
address?string
onlistening?() => void

bound: boolean

close(onclose?: () => void): this

Parameters

ParameterTypeDefaultDescription
onclose?() => void

closed: boolean

closing: boolean

connect(port: number, address?: string, onconnect?: () => void): this

Overloads:

connect(port: number, address?: string, onconnect?: () => void): this
connect(port: number, onconnect: () => void): this
connect(opts: DgramSocketConnectOptions, onconnect?: () => void): this

Parameters

ParameterTypeDefaultDescription
portnumber
address?string
onconnect?() => void

connected: boolean

disconnect(): this

dropMembership(group: string, iface?: string | null): void

Parameters

ParameterTypeDefaultDescription
groupstring
iface?string | null

dropSourceSpecificMembership(source: string, group: string, iface?: string | null): void

Parameters

ParameterTypeDefaultDescription
sourcestring
groupstring
iface?string | null

getRecvBufferSize(): number

getSendBufferSize(): number

getSendQueueCount(): number

getSendQueueSize(): number

pause(): this

ref(): this

remoteAddress(): DgramSocketAddress | null

resume(): this

send

send(msg: DgramMessage, offset: number, length: number, port?: number, address?: string, cb?: (err: Error | null, bytes: number) => void): void

Parameters

ParameterTypeDefaultDescription
msgDgramMessage
offsetnumber
lengthnumber
port?number
address?string
cb?(err: Error | null, bytes: number) => void

setBroadcast(flag: boolean): void

Parameters

ParameterTypeDefaultDescription
flagboolean

setMulticastInterface(iface: string): void

Parameters

ParameterTypeDefaultDescription
ifacestring

setMulticastLoopback(flag: boolean): boolean

Parameters

ParameterTypeDefaultDescription
flagboolean

setMulticastTTL(ttl: number): number

Parameters

ParameterTypeDefaultDescription
ttlnumber

setRecvBufferSize(size: number): void

Parameters

ParameterTypeDefaultDescription
sizenumber

setSendBufferSize(size: number): void

Parameters

ParameterTypeDefaultDescription
sizenumber

setTTL(ttl: number): number

Parameters

ParameterTypeDefaultDescription
ttlnumber

type: DgramSocketType | null

unref(): this

Functions

createSocket

createSocket(opts?: DgramSocketOptions | DgramSocketType | null, onmessage?: (message: Buffer, remote: DgramRemoteInfo) => void): DgramSocket

Parameters

ParameterTypeDefaultDescription
opts?DgramSocketOptions | DgramSocketType | null
onmessage?(message: Buffer, remote: DgramRemoteInfo) => void

isIP(host: string): IPFamily | 0

Parameters

ParameterTypeDefaultDescription
hoststring

isIPv4(host: string): boolean

Parameters

ParameterTypeDefaultDescription
hoststring

isIPv6(host: string): boolean

Parameters

ParameterTypeDefaultDescription
hoststring

Constants and variables

constants

constants: {
  state: {
    CONNECTING: number
    CONNECTED: number
    BINDING: number
    BOUND: number
    READING: number
    CLOSING: number
    CLOSED: number
    UNREFED: number
  }
  address: {
    MAX_LENGTH: number
  }
  bind: {
    IPV6ONLY: number
    REUSEADDR: number
    REUSEPORT: number
  }
}

Types

IPFamily

type IPFamily = 4 | 6

DgramMessage

type DgramMessage = string | Buffer | ArrayBufferView | (string | Buffer | ArrayBufferView)[]

DgramRemoteInfo

interface DgramRemoteInfo {
  address: string
  family: `IPv${IPFamily}`
  port: number
  size: number
}

DgramSocketAddress

interface DgramSocketAddress {
  address: string
  family: `IPv${IPFamily}`
  port: number
}

DgramSocketBindOptions

interface DgramSocketBindOptions {
  address?: string
  fd?: number
  port?: number
}

DgramSocketConnectOptions

interface DgramSocketConnectOptions {
  address?: string
  port: number
}

DgramSocketEvents

interface DgramSocketEvents {
  close: []
  connect: []
  error: [err: Error]
  listening: []
  message: [message: Buffer, remote: DgramRemoteInfo]
}

DgramSocketOptions

interface DgramSocketOptions {
  ipv6Only?: boolean
  lookup?: DNSLookup
  readBufferSize?: number
  recvBufferSize?: number
  reuseAddr?: boolean
  reusePort?: boolean
  sendBufferSize?: number
  type?: DgramSocketType | null
}

DgramSocketType

type DgramSocketType = 'udp4' | 'udp6'

Classes

DgramError

class DgramError {
  code: string
}

bare-dgram/constants

Constants and variables

constants.constants

constants: {
  state: {
    CONNECTING: number
    CONNECTED: number
    BINDING: number
    BOUND: number
    READING: number
    CLOSING: number
    CLOSED: number
    UNREFED: number
  }
  address: {
    MAX_LENGTH: number
  }
  bind: {
    IPV6ONLY: number
    REUSEADDR: number
    REUSEPORT: number
  }
}

bare-dgram/errors

Classes

errors.DgramError

See also

On this page

Usage
API
DgramSocket
DgramSocket
addMembership(group: string, iface?: string | null): void
address(): DgramSocketAddress | null
addSourceSpecificMembership(source: string, group: string, iface?: string | null): void
bind(port?: number, address?: string, onlistening?: () => void): this
bound: boolean
close(onclose?: () => void): this
closed: boolean
closing: boolean
connect(port: number, address?: string, onconnect?: () => void): this
connected: boolean
disconnect(): this
dropMembership(group: string, iface?: string | null): void
dropSourceSpecificMembership(source: string, group: string, iface?: string | null): void
getRecvBufferSize(): number
getSendBufferSize(): number
getSendQueueCount(): number
getSendQueueSize(): number
pause(): this
ref(): this
remoteAddress(): DgramSocketAddress | null
resume(): this
send
setBroadcast(flag: boolean): void
setMulticastInterface(iface: string): void
setMulticastLoopback(flag: boolean): boolean
setMulticastTTL(ttl: number): number
setRecvBufferSize(size: number): void
setSendBufferSize(size: number): void
setTTL(ttl: number): number
type: DgramSocketType | null
unref(): this
Functions
createSocket
isIP(host: string): IPFamily | 0
isIPv4(host: string): boolean
isIPv6(host: string): boolean
Constants and variables
constants
Types
IPFamily
DgramMessage
DgramRemoteInfo
DgramSocketAddress
DgramSocketBindOptions
DgramSocketConnectOptions
DgramSocketEvents
DgramSocketOptions
DgramSocketType
Classes
DgramError
bare-dgram/constants
Constants and variables
constants.constants
bare-dgram/errors
Classes
errors.DgramError
See also