Skip to content

gufeijun/rpch-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

rpch-node is the Node.js implementation of the cross-language RPC framework, rpch.

Usage

Let's walk through starting a simple RPC server.

1. Create the IDL file:

create math.gfj:

//math.gfj
service Math{
    int32 Add(int32,int32)
}

Next, compile the IDL file using the hgen compiler:

hgen -dir . -lang node ./math.gfj

This command will generate a math.rpch.js file in the current directory. This file defines the service interfaces that the server needs to implement, as well as the client-side APIs for making RPC calls.

2. Server Implementation

server.js:

// Import the framework library
const rpch = require("rpch");
// Import the file generated by the compiler
const mathRPCH = require("./math.rpch");

// Implement the service by extending the generated Math interface
class MathServiceImpl extends mathRPCH.MathInterface{
    // Implement the Add method
    async Add(arg1, arg2) {
        return arg1 + arg2;
    }
}

// Create the server instance
let svr = rpch.createServer();

// Register the service implementation
mathRPCH.registerMathService(svr, new (MathServiceImpl));

// Start listening for connections
svr.listen(8080, "127.0.0.1", () => {
    console.log("server is listening at 127.0.0.1:8080");
})

As a user, you only need to focus on implementing your services and registering them.

3. Client Implementation

client.js:

// Import the framework library
const rpch = require("rpch");
// Import the file generated by the compiler
const mathRPCH = require("./math.rpch");

// Dial the RPC server
let conn = rpch.dial(8080, "127.0.0.1");

conn.onError(err => {
    console.log(err);
})

// Use the generated constructor to create a Math service client from the connection
let client = new mathRPCH.MathClient(conn);

// Make an asynchronous RPC call
(async()=> {
    try {
        // Call the async Add method
        let res = await client.Add(-1, 2);
        if (res != 1) {
            console.log(`want ${1} but got ${res}`);
            process.exit(1);
        }
        console.log("test success!");
    } catch (e) {
        console.log(e);
    } finally {
        conn.destroy();
    }
})()

After establishing a connection, the client uses the auto-generated constructor to create a service-specific client object. This object is bound with all the methods needed to access the remote service.

For more examples, see the examples directory.

Important Considerations

  • Please do not use the stream IDL type with rpch-node. Streaming is currently only supported by the rpch-go implementation.
  • When a service returns or accepts an object with integer members, you must ensure that the Node.js implementation uses parseInt() on all integer members. This is necessary to convert potential floating-point numbers to integers. Failure to do so will result in errors during cross-language communication.

Installation

npm i rpch

Alternatively, you can download the rpch.js file directly from this repository and include it in your project.

references

  • rpch-c:The C implementation of the rpch framework。
  • rpch-go:The Go implementation of the rpch framework。
  • rpch-node:The Node.js implementation of the rpch framework。

About

The Node.js implementation of the rpch framework.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors