$ gnpm install udx-native
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()
})
const udx = new UDX()Creates a new UDX instance.
const bool = UDX.isIPv4(host)Returns true if host is an IPv4 address.
const bool = UDX.isIPv6(host)Returns true if host is an IPv6 address.
const family = UDX.isIP(host)Returns the address family (4 or 6). Returns 0 if invalid.
const socket = udx.createSocket()Creates a new socket instance.
socket.udxIt's the UDX instance from where the socket was created.
socket.streamsIt's a Set that tracks active streams (connected to the socket but not closed).
socket.userDataOptional custom userData. Default is null.
socket.boundIndicates if it's bound to any port. It will be true after a successful bind().
socket.closingIt will be true after close() is called.
socket.idleIndicates that the socket doesn't have any connected stream.
socket.busyIndicates that the socket have at least one connected stream.
socket.address()Returns an object like { host, family, port }. Only available after bind().
socket.bind([port], [host])The default port is 0.
If no host specified: it binds to IPv6 ::. If fails then IPv4 0.0.0.0.
await socket.close()It unbinds the socket so it stops listening for messages.
socket.setTTL(ttl)Sets the amount of times that a packet is allowed to be forwarded through each router or gateway before being discarded.
socket.getRecvBufferSize()socket.setRecvBufferSize()socket.getSendBufferSize()socket.setSendBufferSize()await socket.send(buffer, port, [host], [ttl])Sends a message to port and host destination. Default host is 127.0.0.1.
socket.trySend(buffer, port, [host], [ttl])Same behaviour as send() but no promise.
socket.on('message', (msg, from) => {})msg is a buffer that containts the message.
from is an object like { host, family, port }.
socket.on('close', onclose)Emitted if the socket was ever bound and it got closed.
socket.on('idle', onidle)Emitted if the socket becomes idle (no active streams).
socket.on('busy', onbusy)Emitted if the socket becomes busy (at least one active stream).
socket.on('listening', onlistening)Emitted after a succesfull bind() call.
const stream = udx.createStream(id, [options])Creates a new stream instance that is a Duplex stream.
Available options:
{
firewall: (socket, port, host) => true,
framed: false,
seq: 0
}
stream.udxIt's the UDX instance from where the stream was created.
stream.socketRefers to the socket that is connected to. Setted when you connect() the stream.
stream.idCustom stream id.
stream.remoteIdRemote stream id. Setted when you connect() the stream.
stream.remoteIdRemote stream id. Setted when you connect() the stream.
stream.remoteHostRemote host. Setted when you connect() the stream.
stream.remoteFamilyRemote family (4 or 6). Setted when you connect() the stream.
stream.remotePortRemote port. Setted when you connect() the stream.
stream.userDataOptional custom userData. Default is null.
stream.connectedIndicates if the stream is connected to a socket. It becomes false if the stream is closed.
stream.mtuIndicates the maximum size of each packet.
stream.rttstream.cwndstream.inflightstream.localHostIndicates the connected socket host address. By default null if not connected.
stream.localFamilyIndicates the connected socket family address (4 o 6). By default 0 if not connected.
stream.localPortIndicates the connected socket port. By default 0 if not connected.
stream.setInteractive(bool)stream.setMTU(mtu)Sets the maximum size of each packet.
stream.connect(socket, remoteId, port, [host], [options])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
}
stream.relayTo(destination)Relay stream to another stream.
await stream.send(buffer)Send a message to another stream. Returns a promise.
stream.trySend(buffer)Send a message to another stream.
stream.on('connect', onconnect)Emitted after the stream is connected to a socket.
stream.on('message', onmessage)Emitted if the stream receives a message.
stream.on('mtu-exceeded', onmtuexceeded)Emitted only once if you write data that exceeds the MTU.
const interfaces = udx.networkInterfaces()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 }
]
const watcher = udx.watchNetworkInterfaces([onchange])Listens to changes in the network interfaces. The watcher object is iterable.
watcher.interfacesArray of network interfaces.
watcher.watch()Starts watching for changes. By default it already does it. This is only useful after you unwatch().
watcher.unwatch()Stops watching for changes.
await watcher.destroy()Closes the watcher.
watcher.on('change', onchange)Emitted after a network interface change.
watcher.on('close', onclose)Emitted after the watcher is closed.
const address = await udx.lookup(host, [options])It does a DNS lookup for the IP address. Returns { host, family }.
Available options:
{
family: 0 // => 0, 4 or 6
}
MIT
Copyright 2013 - present © cnpmjs.org | Home |