> ## 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") | Polygon

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

The ethers.js subscription equivalent to [eth\_newPendingTransactionFilter](https://docs.chainstack.com/reference/polygon-newpendingtransactionfilter.md). `subscribe("pendingTransactions")` allows developers to subscribe to real-time updates about pending transactions on the Polygon blockchain; the application will receive notifications whenever a pending transaction appears on the blockchain.

## 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

> [!NOTE]
>
> Note that ethers.js subscriptions require a WebSocket connection.

Use the [provider events](https://docs.ethers.org/v6/api/providers/#ProviderEvent) to attach listeners to the provider:

* `"pending"` — activates for each new pending transaction.
* `provider.on` — registers an event listener for the specified event.
* `provider.off` — removes a previously registered event listener.
* `provider.removeAllListeners` — removes all listeners for an event and stops the subscription.

```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);
        console.log("Subscribed to pending transactions");
    } catch (error) {
        console.error(`Error subscribing to new pending transactions: ${error}`);
    }
}

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

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

// Listen for any provider errors
provider.on("error", handleError);

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

subscribeToPendingTransactions();
```

## 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 MATIC.

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

```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);
        console.log("Subscribed to pending transactions");
    } catch (error) {
        console.error(`Error subscribing to new pending transactions: ${error}`);
    }
}

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

/* Callback 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);
        const from = transaction.from;
        const to = transaction.to;
        const value = transaction.value;
        if (value >= ethers.parseEther("100")) { // 100 MATIC in wei
            console.log(`----- New pending transaction ------`);
            console.log(`From: ${from}`)
            console.log(`To: ${to}`)
            console.log(`Value: ${Number(ethers.formatEther(value)).toFixed(2)} MATIC \n`)
        }
    } catch (error) {
        console.error(`Error getting transaction: ${error}`);
    }
}

// Listen for any provider errors
provider.on("error", handleError);

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

subscribeToPendingTransactions();
```

This code creates a new subscription to the `pending` event by attaching a listener with the `provider.on` method. Each time a new pending transaction appears, the provider invokes the registered callback with the transaction hash.

The code defines two event listener functions: `handleNewPending` and `handleError`. The `handleNewPending` function is called when a new pending transaction is received; it runs the [eth\_getTransactionByHash](https://docs.chainstack.com/reference/polygon-gettransactionbyhash.md) method and extracts the `from`, `to`, and `value` fields. If the value transferred is above 100 MATIC, 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. It calls `provider.removeAllListeners` to remove the listeners and stop the subscription.

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

Finally, the code calls the `subscribeToPendingTransactions` function, which registers the listener. When a new pending transaction is received, the `handleNewPending` function is called to extract the data and log it to the console.

## Try the `subscribe("pendingTransactions")` RPC method yourself
