The answer i got is -
HTTP servers can be employed to expose RESTful APIs for interacting with the blockchain.
The RPC server is a built-in component in Substrate, providing methods for external clients to interact with the blockchain network.
WebSockets can be used to establish a persistent connection between clients and the Substrate node, enabling real-time data streaming and push notifications. The Substrate RPC server also supports WebSocket connections.
Let's break down the different calls we can make to Substrate using HTTP, RPC, and WebSockets:
- HTTP Server:
HTTP is a stateless protocol, and in the context of Substrate, it is commonly used for simple, read-only operations and off-chain interactions. It offers a RESTful interface, where each HTTP request corresponds to a specific action or query.
Common HTTP calls to Substrate:
- GET /block/{blockHash or blockNumber}: Retrieves information about a specific block by its hash or number.
- GET /extrinsic/{extrinsicHash}: Fetches details about a specific extrinsic (transaction) by its hash.
- GET /storage/{storageKey}: Retrieves the value stored at the specified storage key.
- POST /rpc: Submits extrinsics (transactions) to the Substrate node.
- RPC Server (Remote Procedure Call):
RPC is a protocol used to communicate between a client (often an application) and a server (Substrate node) to execute specific functions and obtain results. Substrate exposes an RPC interface to interact with its blockchain, and it provides a broader range of functionalities compared to HTTP.
Common RPC calls to Substrate:
- chain_getBlock: Gets information about a specific block.
- state_getStorage: Retrieves the value stored at the specified storage key.
- author_submitExtrinsic: Submits an extrinsic (transaction) to the Substrate node.
- system_health: Checks the health and status of the Substrate node.
- WebSockets:
WebSockets offer a full-duplex communication channel over a single, long-lived connection, enabling real-time data streaming and bidirectional communication between the client and the Substrate node. WebSockets are useful for applications that require continuous updates or real-time data from the blockchain.
Common WebSocket calls to Substrate:
- chain_subscribeNewHeads: Subscribes to new block headers as they are added to the blockchain.
- chain_subscribeFinalizedHeads: Subscribes to finalized block headers.
- state_subscribeStorage: Subscribes to changes in a specific storage key.
- author_submitAndWatchExtrinsic: Submits an extrinsic (transaction) and subscribes to its status updates.
Advantages and Disadvantages:
HTTP:
- Advantages: Simple to use and widely supported by various programming languages and libraries. Suitable for read-only operations and public data retrieval.
- Disadvantages: Limited real-time capabilities, not ideal for streaming real-time data.
RPC:
- Advantages: Provides a broader set of functionalities compared to HTTP. Well-suited for various use cases, including extrinsic submission and state queries.
- Disadvantages: Still lacks the real-time capabilities of WebSockets.
WebSockets:
- Advantages: Offers real-time data streaming and bidirectional communication, making it perfect for applications that require continuous updates or interactivity with the blockchain.
- Disadvantages: Slightly more complex to implement than HTTP or RPC, and may not be supported by all clients or libraries.
When developing applications on the Substrate framework, choosing the appropriate communication mechanism (HTTP, RPC, or WebSockets) depends on the specific requirements of the application. For basic read-only interactions, HTTP might suffice. For more complex and real-time use cases, WebSockets could be the preferred choice. Meanwhile, RPC covers a wide range of functionalities, making it a versatile option for many scenarios.
But i am still confussed.
Please provide some practicle examplse.