![Introducing Dashboard Analytics](https://cdn.sanity.io/images/cgdhsj6q/production/af327c5bd94708db501516a75da6870aef894c64-2286x1522.png?w=400&fit=max&auto=format)
Product
Introducing Dashboard Analytics
We're introducing a new Analytics feature in the Socket dashboard so you can view changes in your organization's and repositories' alerts over time.
@metamask/json-rpc-middleware-stream
Advanced tools
A small toolset for streaming JSON-RPC data and matching requests and responses
@metamask/json-rpc-middleware-stream is a package that provides middleware for JSON-RPC communication over streams. It is particularly useful for building applications that need to handle JSON-RPC requests and responses in a streaming context, such as browser extensions or other environments where communication between different contexts is required.
Creating a JSON-RPC Stream
This feature allows you to create a JSON-RPC stream middleware and integrate it with a JSON-RPC engine. The stream can then be used to send and receive JSON-RPC messages.
const { createStreamMiddleware } = require('@metamask/json-rpc-middleware-stream');
const { createEngineStream } = require('json-rpc-engine');
const { JsonRpcEngine } = require('json-rpc-engine');
const engine = new JsonRpcEngine();
const streamMiddleware = createStreamMiddleware();
engine.push(streamMiddleware.middleware);
const stream = createEngineStream({ engine });
// Now you can use the stream for communication
stream.on('data', (data) => {
console.log('Received data:', data);
});
stream.write({ id: 1, jsonrpc: '2.0', method: 'eth_blockNumber', params: [] });
Handling JSON-RPC Requests
This feature demonstrates how to handle JSON-RPC requests using the middleware. In this example, the middleware responds to the 'eth_blockNumber' method with a mock block number.
const { createStreamMiddleware } = require('@metamask/json-rpc-middleware-stream');
const { JsonRpcEngine } = require('json-rpc-engine');
const engine = new JsonRpcEngine();
const streamMiddleware = createStreamMiddleware();
engine.push(streamMiddleware.middleware);
engine.push((req, res, next, end) => {
if (req.method === 'eth_blockNumber') {
res.result = '0x10'; // Example block number
end();
} else {
next();
}
});
const stream = streamMiddleware.stream;
stream.on('data', (data) => {
console.log('Received data:', data);
});
stream.write({ id: 1, jsonrpc: '2.0', method: 'eth_blockNumber', params: [] });
json-rpc-engine is a framework for building JSON-RPC middleware stacks. It allows you to create a series of middleware functions that can process JSON-RPC requests and responses. Compared to @metamask/json-rpc-middleware-stream, json-rpc-engine focuses more on the middleware stack itself rather than the streaming aspect.
web3-provider-engine is a tool for composing Ethereum provider middleware using a JSON-RPC engine. It is commonly used in Ethereum dApps to handle various provider-related tasks. While it also deals with JSON-RPC, it is more specialized for Ethereum and includes additional features for managing provider connections and caching.
A small toolset for streaming JSON RPC data and matching requests and responses. Made to be used with @metamask/json-rpc-engine
.
yarn add @metamask/json-rpc-middleware-stream
or
npm install @metamask/json-rpc-middleware-stream
This package is part of a monorepo. Instructions for contributing can be found in the monorepo README.
FAQs
A small toolset for streaming JSON-RPC data and matching requests and responses
The npm package @metamask/json-rpc-middleware-stream receives a total of 116,985 weekly downloads. As such, @metamask/json-rpc-middleware-stream popularity was classified as popular.
We found that @metamask/json-rpc-middleware-stream demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 11 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're introducing a new Analytics feature in the Socket dashboard so you can view changes in your organization's and repositories' alerts over time.
Security News
A new OpenSSF report uncovers critical gaps in secure software training, with 75% of new developers unfamiliar with secure practices, highlighting urgent educational needs.
Security News
The 2023 Python Developers Survey reveals key trends in packaging, web frameworks, and developer demographics, highlighting a shift toward innovative tools as the Python community diversifies and grows among less experienced developers.