Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
web3-providers-ws
Advanced tools
The web3-providers-ws package is a WebSocket provider for the web3.js library, which allows for real-time communication with Ethereum nodes. It is used to connect to an Ethereum node over WebSocket, enabling functionalities such as subscribing to events and receiving updates in real-time.
Connecting to an Ethereum Node
This feature allows you to connect to an Ethereum node using a WebSocket provider. The code sample demonstrates how to create a WebSocket provider and use it to get the current block number.
const Web3 = require('web3');
const Web3WsProvider = require('web3-providers-ws');
const wsProvider = new Web3WsProvider('wss://mainnet.infura.io/ws/v3/YOUR_INFURA_PROJECT_ID');
const web3 = new Web3(wsProvider);
web3.eth.getBlockNumber().then(console.log);
Subscribing to New Blocks
This feature allows you to subscribe to new block headers. The code sample demonstrates how to set up a subscription to receive updates whenever a new block is mined.
const Web3 = require('web3');
const Web3WsProvider = require('web3-providers-ws');
const wsProvider = new Web3WsProvider('wss://mainnet.infura.io/ws/v3/YOUR_INFURA_PROJECT_ID');
const web3 = new Web3(wsProvider);
web3.eth.subscribe('newBlockHeaders', (error, blockHeader) => {
if (!error) {
console.log(blockHeader);
}
});
Subscribing to Pending Transactions
This feature allows you to subscribe to pending transactions. The code sample demonstrates how to set up a subscription to receive updates whenever a new transaction is pending.
const Web3 = require('web3');
const Web3WsProvider = require('web3-providers-ws');
const wsProvider = new Web3WsProvider('wss://mainnet.infura.io/ws/v3/YOUR_INFURA_PROJECT_ID');
const web3 = new Web3(wsProvider);
web3.eth.subscribe('pendingTransactions', (error, transactionHash) => {
if (!error) {
console.log(transactionHash);
}
});
The web3-providers-http package is another provider for the web3.js library, but it uses HTTP instead of WebSocket. It is suitable for applications that do not require real-time updates. Compared to web3-providers-ws, it is less efficient for real-time data but simpler to set up and use.
The ethers package is a complete and compact library for interacting with the Ethereum blockchain. It includes support for WebSocket providers, similar to web3-providers-ws, but also offers a broader range of functionalities such as contract interaction, wallet management, and utilities for encoding/decoding data. It is often considered more modern and user-friendly compared to web3.js.
This is a sub-package of web3.js.
web3-providers-ws
contains the Web3.js provider for the Websocket protocol.
You can install the package either using NPM or using Yarn
npm install web3-providers-ws
yarn add web3-providers-ws
Script | Description |
---|---|
clean | Uses rimraf to remove dist/ |
build | Uses tsc to build package and dependent packages |
lint | Uses eslint to lint package |
lint:fix | Uses eslint to check and fix any warnings |
format | Uses prettier to format the code |
test | Uses jest to run unit tests |
test:integration | Uses jest to run tests under /test/integration |
test:unit | Uses jest to run tests under /test/unit |
FAQs
Websocket provider for Web3 4.x.x
The npm package web3-providers-ws receives a total of 448,476 weekly downloads. As such, web3-providers-ws popularity was classified as popular.
We found that web3-providers-ws demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 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.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.