What is sync-rpc?
The sync-rpc npm package allows for synchronous remote procedure calls (RPC) in Node.js. It enables the execution of functions in a separate process and returns the result synchronously, which can be useful for certain use cases where asynchronous behavior is not desired.
What are sync-rpc's main functionalities?
Synchronous RPC Call
This feature allows you to make synchronous RPC calls to a worker script. In this example, the worker script (worker.js) contains a function to add two numbers. The main script calls this function synchronously and logs the result.
const syncRpc = require('sync-rpc');
const rpc = syncRpc(__dirname + '/worker.js');
const result = rpc({ method: 'add', params: [1, 2] });
console.log(result); // Outputs: 3
Handling Multiple RPC Methods
This feature demonstrates how to handle multiple RPC methods within the worker script. The main script can call different methods (e.g., 'add' and 'subtract') and receive the results synchronously.
const syncRpc = require('sync-rpc');
const rpc = syncRpc(__dirname + '/worker.js');
const addResult = rpc({ method: 'add', params: [1, 2] });
const subtractResult = rpc({ method: 'subtract', params: [5, 3] });
console.log(addResult); // Outputs: 3
console.log(subtractResult); // Outputs: 2
Error Handling in RPC
This feature shows how to handle errors in synchronous RPC calls. If the worker script throws an error (e.g., division by zero), the main script can catch and handle the error appropriately.
const syncRpc = require('sync-rpc');
const rpc = syncRpc(__dirname + '/worker.js');
try {
const result = rpc({ method: 'divide', params: [1, 0] });
console.log(result);
} catch (error) {
console.error('Error:', error.message);
}
Other packages similar to sync-rpc
node-ipc
The node-ipc package provides a way to communicate between Node.js processes using Inter-Process Communication (IPC). Unlike sync-rpc, node-ipc is designed for asynchronous communication and supports various communication protocols such as TCP, UDP, and Unix Sockets.
zerorpc
zerorpc is a package that allows for RPC communication between different programming languages. It is built on top of ZeroMQ and MessagePack, providing a robust and language-agnostic RPC solution. Unlike sync-rpc, zerorpc supports both synchronous and asynchronous communication.
dnode
dnode is a package for asynchronous RPC in Node.js. It allows for remote method invocation between Node.js processes. Unlike sync-rpc, dnode is designed for asynchronous communication and provides a more flexible and dynamic way to define and call remote methods.
sync-rpc
Run asynchronous commands synchronously by putting them in a separate process
Installation
npm install sync-rpc --save
Usage
worker.js
function init(connection) {
return function (message) {
return Promise.resolve('sent ' + message + ' to ' + connection);
}
}
module.exports = init;
const assert = require('assert');
const rpc = require('sync-rpc');
const client = rpc(__dirname + '/../test-worker.js', 'My Server');
const result = client('My Message');
assert(result === 'sent My Message to My Server');
License
MIT