DocumentationBlog

Streaming pending data

The Apibara streaming protocol supports sending pending data, that is data that has not been produced on the chain yet.

This feature is extremely powerful and unique to Apibara. Developers can use it to add optimistic updates to their application, this makes the application feel faster and more responsive to the users.

Use cases for optimistic updates are:

  • Gaming: update a game's state as players submit their moves.
  • NFT: update a drop status in real-time.
  • DeFi: show prices and liquidity as they will be in the future, not the past.
  • Social: create a smooth user experience.

Requesting pending data#

Request pending data by specifying the pending_block_interval_seconds parameter in the StreamMessages request. Any value greater than 0 will result in the stream sending pending messages after it reaches the chain's head.

Notice that your client may receive multiple pending messages before a new data message is produced.

Best practices for handling pending data#

  • Set pending_block_interval_seconds to a value that makes sense for your application. Receiving pending data every second puts unnecessary compute and bandwidth stress on your machine.
  • Treat pending data as "mini" chain reorganizations. Invalidate the previous pending data before handling the new pending data. The Python SDK does this automatically for you.
  • Invalidate pending data when receiving a new data message. The Python SDK does this automatically for you.