SDK WebSockets Endpoints
How to use WebSockets with the Alchemy SDK
Available Subscription Types
What are WebSockets and how do they differ from HTTP?
WebSockets is a bidirectional communication protocol that maintains a network connection between a server and a client. Unlike HTTP, with WebSockets clients don’t need to continuously make requests when they want information.
Instead, an open WebSocket connection can push network updates to clients by allowing them to subscribe to certain network states, such as new transactions or blocks being added to the blockchain.
Getting Started
The Alchemy SDK is the easiest way to start using WebSockets. It exposes WebSockets support via the Ethers.js syntax. In addition, it exposes an additional Enhanced APIs created by Alchemy.
See an example below:
The Alchemy SDK automatically adds retry handling for WebSocket failures with no configuration necessary.
Alchemy SDK’s Subscription Methods
These are the methods that you can call to begin a subscription. For types of events you can subscribe to, see Alchemy SDK’s Subscription Types.
The Alchemy SDK’s WebSockets is built on top of Ethers.js’s implementation of WebSockets, so the majority of the syntax will be identical.
SDK Subscription Types Support by Chain
Alchemy SDK’s Subscription Types
block (newHeads)
Emitted when a new block is mined.
AlchemySubscription.MINED_TRANSACTIONS
Emitted when a transaction is confirmed onto the blockchain. Alchemy’s custom implementation of this endpoint allows you to filter based on fromAddress
or toAddress
, the address a transaction was sent to or received from.
AlchemySubscription.PENDING_TRANSACTIONS
Emitted when a new transaction enters the memory pool. Alchemy’s custom implementation of this endpoint allows you to filter based on fromAddress
or toAddress
, the address a transaction was sent to or received from.
If you were previously using the newPendingTransactions
method on either Web3.js or Ethers.js, you should use alchemy_pendingTransactions
. It’s a strict superset of the functionality of newPendingTransactions
, and functions identically if you don’t pass in a fromAddress
or toAddress
filter.
transactions
Emitted when a specified transaction has been mined.
logs
A filter is an object, representing a contract log Filter, which has the optional properties address (the source contract) and topics (a topic-set to match).
If address is unspecified, the filter matches any contract address.
Subscription Type Support Per Chain
For the latest Alchemy support by chain for certain WebSockets requests, check our Feature Support By Chain article here.
WebSocket Limits
The following limits apply for WebSocket connections:
- There is a limit of 20,000 WebSocket connections per API Key as well as 1,000 parallel WebSocket subscriptions per WebSocket connection, creating a maximum of 20 million subscriptions per application.
- The maximum size of a JSON-RPC
batch
request that can be sent over a WebSocket connection is 20 - Free tier users will be limited to 10 concurrent requests per WebSocket connection.