WebSockets
Subscriptions instead of polling — for wallets, indexers and anything that waits for events.
Copy the WebSocket URL from the testnet page (under the RPC endpoint). It is signed for that one testnet and is good for 24 hours; reload the page for a fresh one. The same connection answers ordinary calls too.
ts
import { WebSocketProvider } from "ethers";
const provider = new WebSocketProvider(process.env.FORKSTATE_WS!);
provider.on("block", (number) => console.log("new block", number));Subscriptions
| Subscription | Sends |
|---|---|
newHeads | The header of every block the testnet mines |
logs | Logs matching { address, topics } |
newPendingTransactions | The hash of each transaction |
topicsis positional; each position can be a topic,nullfor anything, or a list meaning “any of these”.- A testnet mines as it receives, so a pending transaction is announced when its block is made.
- Up to 64 subscriptions per connection. A ping every 30 seconds keeps an idle one open.
If the account runs out of credit, open sockets are closed with code
4402 and new ones are refused until credit is added.