bare-dgram
Native UDP sockets for JavaScript
Documented against
v1.1.1stable
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-dgramUsage
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
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | DgramSocketOptions | DgramSocketType | null | — | — |
onmessage? | (message: Buffer, remote: DgramRemoteInfo) => void | — | — |
addMembership(group: string, iface?: string | null): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
group | string | — | — |
iface? | string | null | — | — |
address(): DgramSocketAddress | null
addSourceSpecificMembership(source: string, group: string, iface?: string | null): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
source | string | — | — |
group | string | — | — |
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): thisParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
port? | number | — | — |
address? | string | — | — |
onlistening? | () => void | — | — |
bound: boolean
close(onclose?: () => void): this
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
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): thisParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
port | number | — | — |
address? | string | — | — |
onconnect? | () => void | — | — |
connected: boolean
disconnect(): this
dropMembership(group: string, iface?: string | null): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
group | string | — | — |
iface? | string | null | — | — |
dropSourceSpecificMembership(source: string, group: string, iface?: string | null): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
source | string | — | — |
group | string | — | — |
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): voidParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | DgramMessage | — | — |
offset | number | — | — |
length | number | — | — |
port? | number | — | — |
address? | string | — | — |
cb? | (err: Error | null, bytes: number) => void | — | — |
setBroadcast(flag: boolean): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
flag | boolean | — | — |
setMulticastInterface(iface: string): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
iface | string | — | — |
setMulticastLoopback(flag: boolean): boolean
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
flag | boolean | — | — |
setMulticastTTL(ttl: number): number
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
ttl | number | — | — |
setRecvBufferSize(size: number): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
size | number | — | — |
setSendBufferSize(size: number): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
size | number | — | — |
setTTL(ttl: number): number
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
ttl | number | — | — |
type: DgramSocketType | null
unref(): this
Functions
createSocket
createSocket(opts?: DgramSocketOptions | DgramSocketType | null, onmessage?: (message: Buffer, remote: DgramRemoteInfo) => void): DgramSocketParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | DgramSocketOptions | DgramSocketType | null | — | — |
onmessage? | (message: Buffer, remote: DgramRemoteInfo) => void | — | — |
isIP(host: string): IPFamily | 0
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
host | string | — | — |
isIPv4(host: string): boolean
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
host | string | — | — |
isIPv6(host: string): boolean
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
host | string | — | — |
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 | 6DgramMessage
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
- Builds on
bare-dnsandbare-events. - Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.