Native bindings for libutp. For more information about utp read BEP 29.
npm install utp-native
const utp = require('utp-native')
const server = utp.createServer(function (socket) {
socket.pipe(socket) // echo server
})
server.listen(10000, function () {
const socket = utp.connect(10000)
socket.write('hello world')
socket.end()
socket.on('data', function (data) {
console.log('echo: ' + data)
})
socket.on('end', function () {
console.log('echo: (ended)')
})
})
There two APIs available. One that mimicks the net core module in Node as much as possible and another one that allows you to reuse the same udp socket for both the client and server. The last one is useful if you plan on using this in combination with NAT hole punching.
net-like API
Create a new utp server instance.
Options include
{
allowHalfOpen: true // set to false to disallow half open connections
}
Listen for on port. If you don't provide a port or pass in 0
a free port will be used. Optionally you can provide an interface address as well, defaults to 0.0.0.0
.
Returns an address object, {port, address}
that tell you which port / address this server is bound to.
Emitted when the server is listening
Emitted when a client has connected to this server
Emitted when a critical error happened
Closes the server.
Emitted when the server is fully closed. Note that this will only happen after all connections to the server are closed.
Set this property is you want to limit the max amount of connections you want to receive
An array of all the connections the server has.
Opposite of unref.
Unreferences the server from the node event loop.
Create a new client connection. host defaults to localhost. The client connection is a duplex stream that you can write / read from.
Options include:
{
allowHalfOpen: true // set to false to disallow half open connections
}
Similar to server.address
.
The address of the remote peer.
The port of the remote peer.
If you don't need every packet as soon as they arrive
set connection.setInteractive(false)
.
This might greatly improve performance
Set the expected content size. This will make utp-native
buffer larger chunks of data until size
bytes have been read.
This might greatly improve performance
Set a continuous timeout. If no packets have been received within ms
a timeout event is triggered. Up to you to listen for this event and
potentially destroy the socket. All timeouts are cancelled on socket end.
Emitted when the connection is actually connected.
Emitted when the connection is fully closed.
Emitted if an unexpected error happens.
Forcefully destroys the connection.
In addition to this the connection has all the classic stream methods such as .write
etc.
Note that utp requires the first data message to be sent from the client in a client/server scenario. In most cases this is what happens anyway but something to be aware of. This module will cork the server stream until it receives a client message because of that.
The socket api allows you to reuse the same underlying UDP socket to both connect to other clients on accept incoming connections. It also mimicks the node core dgram socket api.
Create a new utp socket.
Options include:
{
allowHalfOpen: true // set to false to disallow half open connections
}
Bind the socket.
Emitted when the socket is bound.
Send a udp message.
Listen for a udp message.
Close the socket.
Returns an address object, {port, address}
that tell you which port / address this socket is bound to.
Emitted when the socket is fully closed.
Emitted if the socket experiences an error.
Start listening for incoming connections. Performs a bind as well.
Emitted after you start listening and a client connects to this socket. Connection is similar to the connection used in the net api.
Connect to another socket. Connection is similar to the connection used in the net api.
Dereference the socket from the node event loop.
Opposite of socket.unref()
When developing you'll need to install the build tools based on your platform to make node-gyp run. Then run:
npm run fetch-libutp
This will fetch the libutp dependency as a gitsubmodule. Then build it using
npm install
To rebuild it simply do:
node-gyp build
MIT