What is @toruslabs/openlogin-jrpc?
@toruslabs/openlogin-jrpc is a JavaScript library that provides JSON-RPC (Remote Procedure Call) functionalities for interacting with the OpenLogin authentication system. It allows developers to create and manage JSON-RPC connections, handle requests and responses, and integrate with the OpenLogin system for secure authentication and communication.
What are @toruslabs/openlogin-jrpc's main functionalities?
Creating a JSON-RPC connection
This feature allows you to create a JSON-RPC connection using the provided middleware and engine. The code sample demonstrates how to set up a JSON-RPC engine and create a stream for sending and receiving JSON-RPC messages.
const { createStreamMiddleware } = require('@toruslabs/openlogin-jrpc');
const { createEngineStream } = require('@toruslabs/openlogin-jrpc');
const { JsonRpcEngine } = require('@toruslabs/openlogin-jrpc');
const engine = new JsonRpcEngine();
const { middleware, stream } = createStreamMiddleware();
engine.push(middleware);
const rpcStream = createEngineStream({ engine });
// Now you can use rpcStream to send and receive JSON-RPC messages
Handling JSON-RPC requests
This feature allows you to handle JSON-RPC requests by defining middleware functions. The code sample shows how to create a middleware that responds to a specific JSON-RPC method ('example_method') with a predefined result.
const { JsonRpcEngine } = require('@toruslabs/openlogin-jrpc');
const engine = new JsonRpcEngine();
engine.push((req, res, next, end) => {
if (req.method === 'example_method') {
res.result = 'example_result';
end();
} else {
next();
}
});
// Now the engine can handle JSON-RPC requests with the method 'example_method'
Sending JSON-RPC requests
This feature allows you to send JSON-RPC requests and handle the responses. The code sample demonstrates how to create a JSON-RPC request and send it through the rpcStream, then handle the response.
const { createEngineStream, JsonRpcEngine } = require('@toruslabs/openlogin-jrpc');
const { createStreamMiddleware } = require('@toruslabs/openlogin-jrpc');
const engine = new JsonRpcEngine();
const { middleware, stream } = createStreamMiddleware();
engine.push(middleware);
const rpcStream = createEngineStream({ engine });
const request = { jsonrpc: '2.0', id: 1, method: 'example_method', params: [] };
rpcStream.write(request, (err, response) => {
if (err) {
console.error(err);
} else {
console.log(response.result); // 'example_result'
}
});
Other packages similar to @toruslabs/openlogin-jrpc
json-rpc-engine
json-rpc-engine is a library for creating and managing JSON-RPC engines. It provides similar functionalities for handling JSON-RPC requests and responses, but it does not specifically integrate with the OpenLogin system. It is more general-purpose and can be used in various JSON-RPC scenarios.
jayson
jayson is a full-featured JSON-RPC 2.0/1.0 client and server for Node.js. It provides comprehensive tools for creating JSON-RPC servers and clients, handling requests and responses, and supporting both HTTP and WebSocket transports. Unlike @toruslabs/openlogin-jrpc, it is not specifically designed for OpenLogin integration.
jsonrpc-lite
jsonrpc-lite is a lightweight JSON-RPC 2.0 library for Node.js and the browser. It focuses on simplicity and ease of use, providing basic functionalities for creating and parsing JSON-RPC messages. It does not offer the same level of integration with authentication systems like OpenLogin as @toruslabs/openlogin-jrpc does.
OpenLogin JRPC
OpenLogin is a plug & play authentication suite that combines the simplicity of passwordless authentication with the security of non-custodial public key infrastructure (PKI).
OpenLogin JRPC contains rpc method implementations that are used on openlogin for managing authentication flows for the user.
Head over to docs to get started.