udx is reliable, multiplexed, and congestion-controlled streams over udp.
npm i udx-native
It's a transport protocol, made only for peer-to-peer networking.
No handshakes. No encryption. No features. This is good for P2P.
Just fast streams and messages that are composable into powerful things.
const UDX = require('udx-native')
const u = new UDX()
const a = u.createSocket()
const b = u.createSocket()
b.on('message', function (message) {
console.log('received', message.toString())
a.close()
b.close()
})
b.bind(0)
a.send(Buffer.from('hello'), b.address().port)
const UDX = require('udx-native')
const u = new UDX()
const socket1 = u.createSocket()
const socket2 = u.createSocket()
socket1.bind()
socket2.bind()
const stream1 = u.createStream(1)
const stream2 = u.createStream(2)
stream1.connect(socket1, stream2.id, socket2.address().port, '127.0.0.1')
stream2.connect(socket2, stream1.id, socket1.address().port, '127.0.0.1')
stream1.write(Buffer.from('hello'))
stream1.end()
stream2.on('data', function (data) {
console.log(data)
})
stream2.on('end', function () {
stream2.end()
})
stream1.on('close', function () {
console.log('stream1 closed')
socket1.close()
})
stream2.on('close', function () {
console.log('stream2 closed')
socket2.close()
})
Creates a new UDX instance.
Returns true
if host is an IPv4 address.
Returns true
if host is an IPv6 address.
Returns the address family (4
or 6
). Returns 0
if invalid.
Creates a new socket instance.
Available options
:
{
ipv6Only: false,
reuseAddress: false
}
It's the UDX instance from where the socket was created.
It's a Set
that tracks active streams (connected to the socket but not closed).
Optional custom userData. Default is null
.
Indicates if it's bound to any port. It will be true
after a successful bind()
.
It will be true
after close()
is called.
Indicates that the socket doesn't have any connected stream.
Indicates that the socket have at least one connected stream.
Returns an object like { host, family, port }
. Only available after bind()
.
The default port is 0
.
If no host specified: it binds to IPv6 ::
. If fails then IPv4 0.0.0.0
.
It unbinds the socket so it stops listening for messages.
Sets the amount of times that a packet is allowed to be forwarded through each router or gateway before being discarded.
Sends a message to port and host destination. Default host is 127.0.0.1
.
Same behaviour as send()
but no promise.
msg
is a buffer that containts the message.
from
is an object like { host, family, port }
.
Emitted if the socket was ever bound and it got closed.
Emitted if the socket becomes idle (no active streams).
Emitted if the socket becomes busy (at least one active stream).
Emitted after a succesfull bind()
call.
Creates a new stream instance that is a Duplex stream.
Available options
:
{
firewall: (socket, port, host) => true,
framed: false,
seq: 0
}
It's the UDX instance from where the stream was created.
Refers to the socket that is connected to. Setted when you connect()
the stream.
Custom stream id.
Remote stream id. Setted when you connect()
the stream.
Remote stream id. Setted when you connect()
the stream.
Remote host. Setted when you connect()
the stream.
Remote family (4
or 6
). Setted when you connect()
the stream.
Remote port. Setted when you connect()
the stream.
Optional custom userData. Default is null
.
Indicates if the stream is connected to a socket. It becomes false
if the stream is closed.
Indicates the maximum size of each packet.
Indicates the connected socket host address. By default null
if not connected.
Indicates the connected socket family address (4
o 6
). By default 0
if not connected.
Indicates the connected socket port. By default 0
if not connected.
Connects the stream using a socket to a: remote stream id, and remote socket port/host.
If no host specified it uses 127.0.0.1
by default.
Available options
:
{
ack
}
Change the remote end of the stream.
If no host specified it uses 127.0.0.1
by default.
Relay stream to another stream.
Send a message to another stream. Returns a promise.
Send a message to another stream.
Wait for pending stream writes to have been explicitly acknowledged by the other side of the connection.
Emitted after the stream is connected to a socket.
Emitted if the stream receives a message.
Emitted when the remote end of the stream changes.
Emitted only once if you write data that exceeds the MTU.
Returns an array of network interfaces, for example:
[
{ name: 'lo', host: '127.0.0.1', family: 4, internal: true },
{ name: 'enp4s0', host: '192.168.0.20', family: 4, internal: false },
{ name: 'lo', host: '::1', family: 6, internal: true },
{ name: 'enp4s0', host: 'df08::c8df:bf61:95c1:352b', family: 6, internal: false }
]
Listens to changes in the network interfaces. The watcher
object is iterable.
Array of network interfaces.
Starts watching for changes. By default it already does it. This is only useful after you unwatch()
.
Stops watching for changes.
Closes the watcher.
Emitted after a network interface change.
Emitted after the watcher is closed.
It does a DNS lookup for the IP address. Returns { host, family }
.
Available options
:
{
family: 0 // => 0, 4 or 6
}
To develop UDX locally, you need to create a libudx prebuild. bare-make is used for this.
Requirements: The Clang C-compiler should be installed.
The other setup steps are:
npm install -g bare-runtime bare-make
npm install
bare-make generate
bare-make build
bare-make install
When testing changes, rebuild the prebuilds:
bare-make generate
bare-make build
bare-make install
Apache-2.0