Allows clients to wait for events instead of polling for them. It works by subscribing to particular events. The node will return a subscription id. For each event that matches the subscription a notification with relevant data is send together with the subscription id.

Subscription methods

Subscription methods are available for WebSocket connections only, and allow you to wait for events instead of polling for them. For example, dapps can subscribe to logs and receive notifications when a specific event occurs.

The following subscription methods are available:

  1. eth_subscribe - Create a subscription to a particular event
  2. eth_unsubscribe - Cancel an active subscription

What is Websocket?

WebSocket is a communication protocol that provides full-duplex communication channels over a single, long-lived connection between a client and a server. Unlike the traditional request-response model of HTTP, WebSocket enable real-time, bidirectional data exchange, allowing both the client and server to send and receive data at any time without the overhead of repeatedly establishing new connections.

Using JSON-RPC Requests to Access WebSocket

The eth_subscribe and eth_unsubscribe JSON-RPC methods allow you to access WebSocket.

Install a command line tool for making WebSocket requests, or to make requests from your project files instead of the command line. Using wscat, you can send requests as follows:

Example Request

// create subscription
{"id": 1, "method": "eth_subscribe", "params": ["newHeads"]}
{
    "jsonrpc": "2.0",
    "id": 1,
    "result": "0x969993ad38ffa8c6039a574965fb53d0"
}

// cancel subscription
{"id": 1, "method": "eth_unsubscribe", "params": ["0x969993ad38ffa8c6039a574965fb53d0"]}
{"jsonrpc":"2.0","id":1,"result":true}

Compatibility

Axiomesh currently supports subscription methods such as newHeads, logs, and newPendingTransactions, making it compatible with most Ethereum subscription mechanisms. However, it does not support the syncing subscription. If you require information about the synchronization status of a node, you can directly query this information through the HTTP endpoint eth_syncing instead.