Low level TCP library for Node.js
npm install turbo-net
const turbo = require('turbo-net')
// Echo server that allocates constant memory
const server = turbo.createServer(function (socket) {
socket.read(Buffer.alloc(32 * 1024), function onread (err, buf, read) {
if (err) throw err
socket.write(buf, read, function (err) {
if (err) throw err
socket.read(buf, onread)
})
})
})
server.listen(8080, function () {
const socket = turbo.connect(8080)
socket.read(Buffer.alloc(32), function (err, buf, read) {
if (err) throw err
console.log(buf.toString('utf-8', 0, read))
})
socket.write(Buffer.from('hello world\n'))
})
Running the echo server examples in ./examples
I get the following throughput on my laptop
- echo-classic: 1.3GB/s at ~100MB of ram
- echo-turbo: 3.4GB/s at ~35MB of ram
Create a new TCP server. Options include:
{
allowHalfOpen: false, // set to true to allow half open TCP connections
reusePort: true // Disable if you don't want SO_REUSEPORT to be set (SO_REUSEADDR on windows)
}
Emitted when a new connection is established.
Emitted when the server is listening.
Unordered array containing the current active connections
Listen on a port.
Similar to net.Server.address. Useful if you are listening on port 0, to find out which port was picked.
Close the server.
Connect to a TCP server. Options include:
{
allowHalfOpen: false // set to true to allow half open TCP connections
}
Emitted when a client connection is fully connected.
Emitted when a client fails to connect.
Emitted a connection is fully closed. No other events will be emitted after.
Emitted when the writable side is fully closed.
Emitted when the readable side is fully closed.
Emitted when the timeout it's reached. Is only a notification, you should end the connection.
Closes the connection.
Read data from the connection. Data will be read into the buffer you pass.
The callback is called with callback(err, buffer, bytesRead)
.
If bytesRead
is 0
, then the readable side of the connection has ended.
Write data to the connection. Optionally you specify how many bytes in the buffer you want to write.
The callback is called with callback(err, buffer, length)
.
Write more than one buffer at once. Optionally you can specify how many bytes in each buffer you want to write.
The callback is called with callback(err, buffers, lengths)
.
End the writable side of the connection.
Set timeout on the connection. Optionally you can provide an callback. If millis is setted on 0, then it disabled.
The IP address of the connection. For example, '172.217.28.163'.
The IP family of the connection. Before the event "connect" or "connection" it's a empty string and after it's always "IPv4".
The remote port. For example, 63750.
MIT