Get your own node endpoint today
Start for free and get your app to production levels immediately. No credit card required.You can sign up with your GitHub, X, Google, or Microsoft account.Parameters
string— a keyword identifying the type of event to subscribe to,logsin 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:errorandresult. 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
-
object— the following sync object when the node is currently syncing:startingBlock— the block number from which the node began syncing.currentBlock— the latest block number the node has synced to.highestBlock— the estimated highest block number that needs to be synced.pulledStates— the number of state entries that have already been downloaded.knownStates— the estimated number of state entries to be downloaded during the sync.
-
boolean— returnsFalsewhen the node is already in sync.
subscribe("syncing") code example
Note that ethers.js subscriptions require a WebSocket connection.
WebSocketProvider and the provider event listeners to react to sync status updates:
provider.send("eth_syncing", [])— queries the current sync status on demand.provider.on("block", ...)— activates for each new block, so you can re-check the sync status in real time.provider.on("error", ...)— activates if an error is detected on the connection.provider.destroy()— closes the connection and removes all listeners.
Use case
A practical use case forsubscribe("syncing") is a DApp that continuously listens for the status of a node and notifies the developer if the node falls behind a certain number of blocks.
The following is an implementation of this concept using ethers.js subscriptions, this program will leave a notification in the console if the node falls more than 100 blocks behind.
WebSocketProvider. It listens for new blocks with provider.on("block", ...) and queries the current sync status on each block with provider.send("eth_syncing", []).
The code defines three event listener functions that are attached to the provider: handleConnected, handleSync, and handleError. The handleConnected function is called when the connection is ready, and it logs a message. The handleSync function is called on every new block, reads the currentBlock and the highestBlock fields from the eth_syncing result, and then compares them. If the node is more than 1,000 blocks behind, the user will receive an alert.
The handleError function is called when an error occurs, and it logs an error message.
Finally, the code calls the subscribeToSync function, which attaches the event listeners. When a new block is received, the handleSync function is called to query the sync status and log it to the console.