node-kcp-x
v1.2.3
Published
KCP protocol for Node.js
Readme
node-kcp-x
KCP Protocol for Node.js
A fork of node-kcp, rewritten using node-addon-api
Features
- High Performance: 30%-40% reduction in average RTT compared to traditional ARQ protocols like TCP
- Low Latency: Maximum RTT reduced by 3x compared to TCP
- Lightweight: Distributed as a single source file
- Modern API: Built with node-addon-api for better Node.js compatibility
- Cross Platform: Supports Windows, Linux, and macOS
Installation
npm install node-kcp-xBuilding from Source
Prerequisites
npm install -g node-gyp prebuildifyBuild Steps
git clone https://github.com/bruce48x/node-kcp
cd node-kcp
npm i
npm run buildUsage Examples
Example 1: Basic UDP Test
npm run install
node test/udpserver.js
node test/udpclient.jsExample 2: Client-Server Communication
# Run server
node examples/server.js
# Run client
node examples/client.jsExample 3: Stream Mode
# Run stream server
node examples/stream-server.js
# Run stream client
node examples/stream-client.jsAPI Reference
KCP Class
const kcp = require('node-kcp-x');
const Kcp = kcp.KCP;
// Create KCP instance
const kcpObj = new Kcp(conv, context);
// Set output callback
kcpObj.output((data, size, context) => {
// Send data to network
});
// Send data
kcpObj.send(buffer);
// Receive data
const buffer = kcpObj.recv();
// Update KCP state
kcpObj.update(timestamp);
// Check next update time
const nextUpdate = kcpObj.check(timestamp);About Pomelo and Pomelo-kcp
If you want to use node-kcp in pomelo server, you need pomelo-kcp
License
Apache-2.0
