3

In the context of the Substrate framework, I'm trying to understand the differences between an HTTP server, an RPC server, and WebSockets. Specifically, how do these communication mechanisms relate to Substrate and what are their use cases within the framework? How does each of them facilitate interactions with the Substrate blockchain network? Are there any advantages or disadvantages of using one over the others? I'm looking for insights into the technical aspects and practical implications of choosing between an HTTP server, an RPC server, or WebSockets when developing applications on the Substrate framework.

To assist you better, I would like to clarify that the calls made to Substrate can be categorized based on the protocols used: HTTP, RPC, and WS. It would be greatly appreciated if you could provide an explanation along with a separate list for each category. Additionally, it would be helpful to understand the rationale behind categorizing them separately.

3 Answers 3

2

An RPC is the thing you want to do, e.g. get the chain ID. As you can see this RPC is called chainId in the group eth.

You have two ways to connect to the server and do these RPCs. You can convert an RPC calls into an HTTP request so you can send it. The procedure is documented somewhere else. You turn it into a JSON object then send the JSON in an HTTP POST request. The request looks like this (I did not test it so there could be a mistake):

POST / HTTP/1.1
Host: name.of.server
Content-Type: application/json-rpc
Content-Length: 63

{"jsonrpc":"2.0", "method": "eth_chainId", "params": [], "id":0}

and the response is like:

HTTP/1.1 200 OK
Content-Type: application/json-rpc
Content-Length: 56

{"jsonrpc":"2.0", "result": 1234, "error": null, "id": 0}

A different way to connect to the server and call RPCs is to use websockets. The websocket protocol is more complicated - you start making an HTTP connection and then convert it to a websocket connection, and then you can send a lot of RPC calls without having to make a new connection and send a bunch of extra metadata each time.

2

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:

  1. 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.
  1. 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.
  1. 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.

0

Example for HTTP Server : Assume you have developed a decentralized social media application using Substrate. You can set up an HTTP server that exposes an API endpoint /posts that allows users to retrieve the latest posts from the blockchain. The server can handle HTTP GET requests to /posts and respond with a JSON array containing the posts data.

Example For RPC Server : Let's say you have created a substrate-based supply chain management system. You can create an RPC server that exposes methods like getProductDetails(productId) and createShipment(orderId, destination). External clients can make RPC calls to these methods, passing parameters needed to retrieve product details or create new shipments in the supply chain.

Example For WebSocket : Imagine that you have developed a real-time trading platform on Substrate. You can establish a WebSocket connection between the trading platform client and the Substrate node. The server can transmit real-time updates, such as new trade orders or price updates, to the client via a WebSocket connection. This allows clients to receive instant notifications and update the trading interface accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.