> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainstack.com/llms.txt
> Use this file to discover all available pages before exploring further.

# subscribe ("pendingTransactions") | Gnosis

> Reference docs for the subscribe ("pendingTransactions") JSON-RPC method on the Gnosis blockchain, available via Chainstack JSON-RPC nodes.

Gnosis Chain ethers.js subscription equivalent to [eth\_newPendingTransactionFilter](/reference/gnosis-newpendingtransactionfilter). `subscribe("pendingTransactions")` allows developers to subscribe to real-time updates about new block headers on the gnosis blockchain; the application will receive notifications whenever a new block is added to the blockchain. The notification will include information about the new block, such as its block number, hash, and timestamp.

## Parameters

* `string` — a keyword identifying the type of event to subscribe to, `pendingTransactions` in this case.
* `function` — (optional) a callback function that will be called every time a new event of the specified type is received. This function takes two parameters: `error` and `result`. The error parameter contains any error that occurred while subscribing to the event, and the result parameter contains the data for the event that was received.

## Response

* `string` — the hash identifying the pending transaction.

## `subscribe("pendingTransactions")` code example

<Info>
  Note that ethers.js subscriptions require a WebSocket connection.
</Info>

Use the `WebSocketProvider` event listeners to react to subscription events:

* `"pending"` — activates for each new pending transaction hash received.
* `provider.on("error", ...)` — activates if an error is detected during the subscription.
* `provider.removeAllListeners("pending")` — unsubscribes from the pending transactions event.

<CodeGroup>
  ```javascript index.js theme={"system"}
  const { ethers } = require("ethers");
  const NODE_URL = "CHAINSTACK_WSS_URL";
  const provider = new ethers.WebSocketProvider(NODE_URL);

  async function subscribeToPendingTransactions() {
      try {
          // Subscribe to the 'pending' event to receive new pending transaction hashes
          provider.on("pending", handleNewPending);

          // Attach an error listener to the provider
          provider.on("error", handleError);

          console.log("Subscribed to pending transactions");
      } catch (error) {
          console.error(`Error subscribing to new pending transactions: ${error}`);
      }
  }

  /* Fallback functions to react to the different events */

  // Event listener that logs the received pending transaction hashes
  function handleNewPending(transactionHash) {
      console.log(transactionHash);
  }

  // Event listener that logs any errors that occur
  function handleError(error) {
      console.error(`Error receiving new blocks: ${error}`);
  }

  subscribeToPendingTransactions();
  ```
</CodeGroup>

## Use case

A practical use case for `subscribe("pendingTransactions")` is a DApp that continuously listens for new pending transactions, then isolates the `from`, `to`, and `value` fields for analytics purposes. This is useful, for example, to only track transactions that move at least a certain amount of xDAI.

The following is an implementation of this concept using ethers.js subscriptions:

<CodeGroup>
  ```javascript index.js theme={"system"}
  const { ethers } = require("ethers");
  const NODE_URL = "CHAINSTACK_WSS_URL";
  const provider = new ethers.WebSocketProvider(NODE_URL);

  async function subscribeTopendingTransactions() {
      try {
          // Subscribe to the 'pending' event to receive new pending transaction hashes
          provider.on("pending", handleNewPending);

          // Attach an error listener to the provider
          provider.on("error", handleError);

          console.log("Subscribed to pending transactions");
      } catch (error) {
          console.error(`Error subscribing to new blocks: ${error}`);
      }
  }

  // Use this function to unsubscribe and exit the program
  function unsubscribe() {
      provider.removeAllListeners("pending");
      console.log("Successfully unsubscribed!");
      return process.exit(1);
  }

  /* Fallback functions to react to the different events */

  // Event listener that logs the received pending transactions and extracts from, to, and value fields
  async function handleNewPending(transactionHash) {
      try {
          const transaction = await provider.getTransaction(transactionHash);
          if (!transaction) return; // The transaction may no longer be in the mempool
          const from = transaction.from;
          const to = transaction.to;
          const value = transaction.value;
          if (value >= 100000000000000000000n) { // 100 xDAI in wei
              console.log(`----- New pending transaction ------`);
              console.log(`From: ${from}`)
              console.log(`To: ${to}`)
              console.log(`Value: ${Number(ethers.formatEther(value)).toFixed(2)} xDAI\n`)
          }
      } catch (error) {
          console.error(`Error getting transaction: ${error}`);
      }
  }

  // Event listener that logs any errors that occur
  function handleError(error) {
      console.error(`Error receiving new blocks: ${error}`);
  }

  subscribeTopendingTransactions();
  ```
</CodeGroup>

This code creates a new subscription to the `pending` event using the `provider.on` method on a `WebSocketProvider`. This method registers event listeners that are called whenever a new pending transaction hash is received.

The code defines two event listener functions that are attached to the provider: `handleNewPending` and `handleError`. The `handleNewPending` function is called when a new pending transaction is received; it runs the [eth\_getTransactionByHash](/reference/gnosis-gettransactionbyhash) method via `provider.getTransaction` and extracts the `from`, `to`, and `value` fields. If the value transferred is above 100 xDAI, the data is logged.

The code includes the `unsubscribe` function that can be implemented in the logic to unsubscribe and exit the program when a condition is met.

The `handleError` function is called when an error occurs, and it logs an error message.

Finally, the code calls the `subscribeTopendingTransactions` function, which creates the subscription and attaches the event listeners. When a new pending transaction is received, the `handleNewPending` function is called to extract the data and log it to the console.
