Skip to content

bruce48x/node-kcp

 
 

Repository files navigation

node-kcp-x

| English | 简体中文 |

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-x

Building from Source

Prerequisites

npm install -g node-gyp prebuildify

Build Steps

git clone https://github.com/bruce48x/node-kcp
cd node-kcp
npm i
npm run build

Usage Examples

Example 1: Basic UDP Test

npm run install
node test/udpserver.js
node test/udpclient.js

Example 2: Client-Server Communication

# Run server
node examples/server.js
# Run client
node examples/client.js

Example 3: Stream Mode

# Run stream server
node examples/stream-server.js
# Run stream client
node examples/stream-client.js

API 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

About

KCP Protocol for Node.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 63.0%
  • C++ 28.8%
  • JavaScript 6.8%
  • Python 1.4%