Using Web3j to Send Ethereum JSON‑RPC Requests via WebSocket
This article explains how to construct and send Ethereum JSON‑RPC calls such as net_version through a WebSocket connection using the Web3j Java library, including source code analysis, request construction, and a practical example with console output.
Web3j Source Code
When first exploring Ethereum's Infura API, the author noticed a WebSocket option but could not locate comprehensive documentation, assuming HTTP was sufficient.
After deeper study of the JSON‑RPC protocol, it became clear that a request could be issued without a dedicated WebSocket API spec.
By examining the Web3j source code, the author pieced together the necessary request‑building logic. The following snippet shows the method used to obtain the network version:
public Request
netVersion() {
return new Request("net_version", Collections.emptyList(), this.web3jService, NetVersion.class);
}The org.web3j.protocol.core.Request constructor is defined as:
public Request(String method, List<S> params, Web3jService web3jService, Class<T> type) {
this.method = method;
this.params = params;
this.id = nextId.getAndIncrement();
this.web3jService = web3jService;
this.responseType = type;
}Sending a request over WebSocket simply involves transmitting a JSON‑RPC string that matches the fields of the Request object:
client.send("{\"jsonrpc\":\"2.0\",\"method\":\"net_version\",\"params\":[],\"id\":1333333}")The author also notes the internal fields of Request , including a globally unique identifier generated with java.util.concurrent.atomic.AtomicLong :
private static AtomicLong nextId = new AtomicLong(0L);
private String jsonrpc = "2.0";
private String method;
private List<S> params;
private long id;
private Web3jService web3jService;
private Class<T> responseType;WebSocket API Practice
Using a custom WebSocket client ( com.funtester.socket.WebSocketFunClient ) connected to the Goerli test network, the author demonstrates a simple main method that sends two JSON‑RPC calls: eth_accounts and net_version .
static final String host = "wss://goerli.infura.io/ws/v3/apikey";
static void main(String[] args) {
def client = new WebSocketFunClient(host, "infura ethereum");
client.connect();
client.send("{\"jsonrpc\":\"2.0\",\"method\":\"eth_accounts\",\"params\":[],\"id\":1}");
client.send("{\"jsonrpc\":\"2.0\",\"method\":\"net_version\",\"params\":[],\"id\":1333333}");
}The console output confirms successful connection and displays the JSON‑RPC responses:
22:03:44.299 WebSocketConnectReadThread-20 infura ethereum received: {"jsonrpc":"2.0","id":1,"result":[]}
22:03:44.544 WebSocketConnectReadThread-20 infura ethereum received: {"jsonrpc":"2.0","id":1333333,"result":"5"}Additional links to original Chinese articles and collections are provided for further reading.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.