Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

web3-providers-ws

Package Overview
Dependencies
42
Maintainers
2
Versions
380
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    web3-providers-ws

Module to handle web3 RPC connections over WebSockets.


Version published
Weekly downloads
483K
increased by1.49%
Maintainers
2
Created
Weekly downloads
 

Package description

What is web3-providers-ws?

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.

What are web3-providers-ws's main functionalities?

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);
  }
});

Other packages similar to web3-providers-ws

Changelog

Source

[1.2.0]

Released with 1.0.0-beta.37 code base.

Readme

Source

web3-providers-ws

This is a sub package of web3.js

This is a websocket provider for web3.js.
Please read the documentation for more.

Installation

Node.js

npm install web3-providers-ws

In the Browser

Build running the following in the web3.js repository:

npm run-script build-all

Then include dist/web3-providers-ws.js in your html file. This will expose the Web3WsProvider object on the window object.

Usage

// in node.js
var Web3WsProvider = require('web3-providers-ws');

var options = { timeout: 30000, headers: {authorization: 'Basic username:password'} } // set a custom timeout at 30 seconds, and credentials (you can also add the credentials to the URL: ws://username:password@localhost:8546)
var ws = new Web3WsProvider('ws://localhost:8546', options);

FAQs

Last updated on 23 Jul 2019

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc