How to Subscribe to Pending Transactions via WebSocket Endpoints
Learn how to subscribe to pending transactions via WebSockets, and filters the transactions based on specified from and/or to addresses.
In this tutorial, you’ll utilize the alchemy_pendingTransactions subscription type API endpoint. If you require the script or further details, refer to the following articles or continue reading for more.
Alchemy provides the most effective method to subscribe to pending transactions, log events, and new blocks using WebSockets on Ethereum, Polygon, Arbitrum, and Optimism. By leveraging modern Web3 libraries, you’re able to access direct subscription types by simply connecting to each endpoint.
In this tutorial, we will test and create a sample project using the alchemy_pendingTransactions
method offered by Alchemy’s WebSocket API.
What relevance does the alchemy_pendingTransactions
provide to users?
- Watching for pending transactions sent to a set of NFT owners to track the most recent floor price
- Watching transactions sent from a whale trader to track trading patterns
How does alchemy_pendingTransactions
compare to newPendingTransactions
? Both these subscription types enable developers to receive transaction hashes that are sent to the network and marked as “pending”. However, alchemy_pendingTransactions
enhance the developer experience by providing filters that can specify based on to/from addresses. This greatly improves the readability of the transaction requests received.
It allows for strengthened requests with specific parameters given by the user including:
toAddress
(optional): Singular address or array of addresses to receive pending transactions sent from this address.fromAddress
(optional): Singular address or array of addresses from receive pending transactions sent from this address.hashesOnly
(optional - default set tofalse
): The response matches the payload of eth_getTransactionByHash. This is information about a transaction by the transaction hash includingblockHash
,blockNumber
andtransactionIndex
.
Step 0: Configure your developer environment
Before you begin, complete the following steps to set up your web3 developer environment.
1. Install Node.js (> 14) on your local machine
2. Install npm on your local machine
3. Install wscat on your local machine
To check your Node version, run the following command in your terminal:
4. Create a free Alchemy account
Step 1: Open your Alchemy App
Once your Alchemy account is created, there will also be a default app that is also created.
To create another Alchemy app, check out this video.
Step 2: Get WebSocket URL from Alchemy App
Once you have created your app, get your WebSocket URL that we will use later in this tutorial.
- Click on your app’s View Key button in the dashboard
- Copy and save the WebSocket URL
Step 3: Output Pending Transactions Using wscat
Wscat is a terminal or shell tool used to connect to the WebSockets server. Each Alchemy application will provide a WebSocket URL that can be used directly with the wscat command.
- Initiate the WebSocket stream
- Enter the specific call command
From your terminal, run the following commands:
If successful, you should see output that looks something like this:
By using wscat, you are able to verify the transaction immediately via the computer’s terminal or shell.
Step 4: Create a Node project
Let’s create an empty repository and install the necessary dependencies for WebSocket connections. We can use either Viem or Ethers.js to manage WebSocket subscriptions.
From your terminal, run the following commands:
This will create a repository named pending-transactions
that holds all the files and dependencies we need.
Open this repo in your preferred code editor, where we’ll write our code in the main.js
file.
Step 5: Output Pending Transactions using WebSocket libraries
Next, we’ll demonstrate how to use Viem or Ethers.js to create an alchemy_pendingTransactions subscription.
To make requests using Alchemy’s pendingTransactions API, we recommend reviewing the alchemy_pendingTransactions docs.
Next, add the following code to the main.js
file, using your Alchemy API key:
Run this script by running the following command in your terminal:
node main.js
If successful, you should see a stream of transactions as the result. This stream of output indicates the latest (pending or mined) transactions hitting the Ethereum Mainnet. It should look something like this:
Step 6: Filter Pending Transactions
Next, we’ll demonstrate how to filter pending transactions based on addresses. While standard WebSocket subscriptions don’t offer built-in filtering, we can implement filtering logic in our application.
Note: The Alchemy-enhanced alchemy_pendingTransactions
API provides native filtering by fromAddress
and toAddress
. For standard WebSocket subscriptions, we need to implement filtering manually as shown below.
Add the following code to the main.js
file, using your Alchemy API key:
Conclusion
You now know how to use WebSocket connections with Viem and Ethers.js to subscribe to pending transactions and filter them based on addresses.
For more advanced filtering capabilities, consider using Alchemy’s enhanced alchemy_pendingTransactions
API which provides native filtering by fromAddress
and toAddress
.
If you enjoyed this tutorial, tweet us at @Alchemy.
If you have any questions or feedback, please contact us at [email protected] or open a ticket in the dashboard.