π A lightweight, fast, and easy-to-use RPC (Remote Procedure Call) engine for Node.js with built-in client and server support.
- β Simple Setup - Get started with just a few lines of code
- β Client & Server - Built-in client and server components
- β Type Flexible - Support for various parameter types (arrays, objects, primitives)
- β Error Handling - Comprehensive error handling and validation
- β Express Integration - Built on top of Express.js
- β CORS Support - Cross-origin requests supported out of the box
- β Lightweight - Minimal dependencies and fast performance
npm install ndk-rpc-engineimport ndk_rpc_server from "ndk-rpc-engine/server";
// Create server instance
const server = new ndk_rpc_server({ port: 4000 });
// Define your functions
const add = ({ a, b }) => a + b;
const subtract = ({ a, b }) => a - b;
const hello = () => "Hello, World!";
// Register functions
await server.register_functions([
{
function_name: "add",
function_block: add,
},
{
function_name: "subtract",
function_block: subtract,
},
{
function_name: "hello",
function_block: hello,
}
]);
// Start the server
await server.start();
console.log("π NDK RPC Server is running!");import { Client } from "ndk-rpc-engine/client";
// Create client instance
const client = new Client({
server_port: 4000
});
// Call remote functions
const response = await client.request({
method: "add",
params: { a: 5, b: 3 }
});
console.log(response.data.result); // Output: 8Creates a new RPC server instance.
Parameters:
options.port(number, optional) - Server port (default: 3000)
Registers an array of functions to be callable via RPC.
Parameters:
functions(Array) - Array of function objects
Function Object Structure:
{
function_name: "string", // Unique function name
function_block: function // The actual function to execute
}Example:
await server.register_functions([
{
function_name: "calculate",
function_block: ({ operation, a, b }) => {
if (operation === "add") return a + b;
if (operation === "multiply") return a * b;
throw new Error("Unknown operation");
}
}
]);Starts the RPC server.
Creates a new RPC client instance.
Parameters:
options.server_port(number, optional) - Server port to connect to (default: 3000)
Makes an RPC call to the server.
Parameters:
options.method(string) - Name of the remote function to calloptions.params(any, optional) - Parameters to pass to the function
Returns:
- Promise resolving to server response
Response Format:
{
statusCode: 200,
message: "Method executed successfully",
data: {
method_name: "function_name",
result: "function_result"
}
}Server:
import ndk_rpc_server from "ndk-rpc-engine/server";
const server = new ndk_rpc_server({ port: 4000 });
const mathOperations = {
add: ({ a, b }) => a + b,
subtract: ({ a, b }) => a - b,
multiply: ({ a, b }) => a * b,
divide: ({ a, b }) => {
if (b === 0) throw new Error("Division by zero!");
return a / b;
}
};
await server.register_functions([
{ function_name: "add", function_block: mathOperations.add },
{ function_name: "subtract", function_block: mathOperations.subtract },
{ function_name: "multiply", function_block: mathOperations.multiply },
{ function_name: "divide", function_block: mathOperations.divide }
]);
await server.start();Client:
import { Client } from "ndk-rpc-engine/client";
const client = new Client({ server_port: 4000 });
// Addition
const addResult = await client.request({
method: "add",
params: { a: 10, b: 5 }
});
console.log("10 + 5 =", addResult.data.result); // 15
// Division with error handling
try {
const divResult = await client.request({
method: "divide",
params: { a: 10, b: 0 }
});
} catch (error) {
console.error("Error:", error.message);
}// Server function
const processArray = (numbers) => {
return numbers.reduce((sum, num) => sum + num, 0);
};
await server.register_functions([
{ function_name: "sum_array", function_block: processArray }
]);
// Client call
const result = await client.request({
method: "sum_array",
params: [1, 2, 3, 4, 5]
});
console.log("Sum:", result.data.result); // 15// Server function
const getServerTime = () => new Date().toISOString();
await server.register_functions([
{ function_name: "server_time", function_block: getServerTime }
]);
// Client call
const result = await client.request({
method: "server_time"
// No params needed
});
console.log("Server time:", result.data.result);The engine provides comprehensive error handling:
try {
const response = await client.request({
method: "nonexistent_function",
params: { a: 1, b: 2 }
});
} catch (error) {
console.error("RPC Error:", error.message);
// Handle different error types
if (error.message.includes("not found")) {
console.log("Function doesn't exist on server");
}
}When you start the server, these endpoints become available:
GET http://localhost:{port}/- Health checkPOST http://localhost:{port}/api/v1/rpc/run-rpc-method- Execute RPC methods
The server comes with CORS enabled by default for all origins. Perfect for web applications.
Built on Express.js, so you can extend it with additional middleware if needed.
MIT Β© Navnath Kadam
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
If you encounter any issues or have questions, please open an issue on GitHub.
Made with β€οΈ by Navnath Kadam
A lightweight RPC (Remote Procedure Call) engine built from scratch in Node.js. It allows you to easily register functions on the server and call them remotely from a client using simple JSON-based requests.
NDK-RPC-Engine provides a minimal and extensible way to:
- Register server-side functions globally.
- Call those functions from clients over HTTP.
- Get structured JSON responses with status codes and results.
This makes it ideal for building microservices, distributed systems, or simple client-server communication without heavy dependencies.
Clone the repository and install dependencies:
git clone https://github.com/yourusername/ndk-rpc-engine.git
cd ndk-rpc-engine
npm installYou can register multiple functions and expose them via the RPC server.
import ndk_rpc_server from "../server/index.mjs";
let server = new ndk_rpc_server({ port: 4000 });
const add = ({a, b}) => a + b;
const subtract = ({a, b}) => a - b;
const hello = () => "Hello, World!";
const isRegistered = await server.register_functions([
{
function_name: "add",
function_block: add,
},
{
function_name: "subtract",
function_block: subtract,
},
{
function_name: "hello",
function_block: hello,
}
]);
await server.start();Server Console Output:
Global Registered function: [object Object]
Registered functions: [
{ function_name: 'add', function_block: [Function: add] },
{ function_name: 'subtract', function_block: [Function: subtract] },
{ function_name: 'hello', function_block: [Function: hello] }
]
NDK-RPC-Engine started at port: http://localhost:4000
Clients can call any registered function on the server.
import { Client } from "../index.mjs";
const client = new Client({
server_port: 4000
});
const response = await client.request({
method : "add",
params: { a: 5 , b: 4 }
});
console.log("Response from server to Client : " , response);Client Console Output:
Response from server to Client : {
message: 'Method executed successfully',
method_name: 'add',
result: 9
}
- Simple function registration (
register_functions). - JSON-based request/response.
- Built-in error handling (e.g., method not found, invalid params).
- Supports multiple clients.
add({a, b})β returns sum.subtract({a, b})β returns difference.hello()β returns greeting string.
- Add WebSocket support for bi-directional communication.
- Support authentication (JWT/OAuth).
- Function namespaces for modularity.
- Middleware hooks (logging, validation, etc.).
Feel free to fork, open issues, and submit PRs.
MIT License Β© 2025 Ndk