eth_subscribe

Subscribe to different Ethereum event types like newHeads, logs, pendingTransactions, and minedTransactions using WebSockets.

🚧

Warning

Note that subscriptions require a WebSocket connection and WebSocket cat for you to use this method in the console.

Install WebSocket cat with:

npm install -g wscat

Method that allows developers to receive real-time notifications regarding new pending transactions on blockchain. The application will receive notifications whenever new pending transactions are identified.

Parameters

  • string: keyword identifying the type of event to subscribe to, newPendingTransactions in this case.

Response

  • subscription: the subscription ID.
  • string: the hash of a pending transaction.

Example

const WebSocket = require('ws');

const webSocket = new WebSocket('your url');

async function subscribeToNewPendingTransactions() {
  
  const request = {
    id: 1,
    jsonrpc: '2.0',
    method: 'eth_subscribe',
    params: ['newPendingTransactions', true],
  };

  const onOpen = (event) => {
    webSocket.send(JSON.stringify(request));
  };

  const onMessage = (event) => {
    const response = JSON.parse(event.data);
    console.log(response);
  };

  try {
    webSocket.addEventListener('open', onOpen);
    webSocket.addEventListener('message', onMessage);
  } catch (error) {
    console.error(error);
  }
}

subscribeToNewPendingTransactions();
wscat -c YOUR_PROVIDER_WEBSOCKET_ENDPOINT
# Wait for the connection to be established

Connected (press CTRL+C to quit)

> {"id":1,"jsonrpc":"2.0","method":"eth_subscribe","params":["newPendingTransactions"]}
Language
Authorization
Header
Click Try It! to start a request and see the response here!