What are WebSockets?
The WebSockets (WSS) communication protocol enables two-way communication between a client and a server over a single TCP connection. The communication protocol maintains a network connection between the two parties, allowing for real-time, low-latency communication. WebSockets allow for ongoing, bidirectional communication unlike HTTP, which is a request-response protocol.
Subscriptions over WebSockets
WebSockets allow you to create stateful subscriptions, which is a type of subscription where the server maintains a record of the client's subscription state. This means that the server remembers what data the client has requested and sent previously, and only sends new data that has changed or meets the client's subscription criteria.
Stateless HTTP WebSocket calls are also supported.
You can create stateful subscriptions over WebSockets to subscribe to specific events on the blockchain. The following
subscription types are available using the
eth_subscribe JSON-RPC API:
newHeadssubscription type emits an event when a new header (block) is added to the chain, including during a chain reorganization.
logssubscription type emits logs that match a specified topic filter and are included in newly added blocks. We strongly recommend specifying a filter when subscribing to the
newPendingTransactionssubscription type subscribes to all pending transactions via WebSockets (regardless if you sent them or not), and returns their transaction hashes.
Infura support subscriptions over WebSockets for the following networks:
WebSocket support is currently in public beta for the Polygon and Linea networks.
WebSockets use cases
Create stateful subscriptions over WebSockets to listen to specific events. Example use cases include:
- Listen when a new NFT is minted by an NFT minting smart contract, this enables your application to update its count of minted NFTs.
- Listen to a smart contract for the latest token price so that price can be reflected immediately on the application to end users.
- Receive updates for cryptocurrency market trades, orders, and Best Bid Offers (BBO).
- Receive the latest token transfers for an address.
- Receive notifications about each new block added to the blockchain.
Subscriptions over WebSockets are currently free on Ethereum. However, subscriptions over WebSockets on Polygon and Linea are not free and will be counted in your daily request limits.
We may change our pricing model and implement some limitations on subscriptions over WebSockets on Ethereum and Polygon in the future. Before implementing any changes, we'll give customers advanced notification, so they can consider the best option for their needs.