Ethereum Geth and Parity IPC in Node.js using JSON-RPC

Jorge Gonzalez Vallejo
3 min readFeb 8, 2019

(updated: 05/27/21)

Intro

So you want to communicate with your local Geth or Parity node over IPC instead of HTTP or Websockets? Inter-process Communication (IPC) is enabled by default — unlike HTTP and Websockets — (edit: this seems to not be true), you can do some cool clustering and worker management with it (which is great for horizontal scaling), and hey it’s your app — why shouldn’t you use IPC?

IPC party all day err day son

Or maybe you’re like me and would rather not involve the network stack. I looked around for a definitive answer whether a loopback is redirected by the OS or if it involves TCP/IP, but I couldn’t find much aside a few old SO answers. I don’t know, maybe it’s a knee-jerk reaction to seeing “HTTP”. Moving on.

JSON-RPC

JSON-RPC is a remote procedure call (RPC) protocol we can use to communicate with Geth, Parity and with the other Ethereum clients as well. Since we’re using Javascript we could use the Web3js library but that’s not what this tutorial is about.

Here’s a link to the Ethereum wiki where you can find a list of JSON-RPC methods: https://eth.wiki/json-rpc/API

Source: Ethereum wiki

Getting Started

By now you hopefully have your Geth or Parity node up and running. By the way, there’s some limitations to using “light” mode. You won’t run into issues for this tutorial, but keep that in mind when you’re adapting this for your usage case.

Once you launch either Geth or Parity you’ll see a file appear in the filesystem at this location:

Linux

Geth: ~/.ethereum/geth.ipc
Parity: ~/.local/share/io.parity.ethereum/jsonrpc.ipc

Mac

Geth: ~/Library/Ethereum/geth.ipc
Parity: ~/Library/Application\ Support/io.parity.ethereum/jsonrpc.ipc

Windows

Geth: ~\Appdata\Roaming\Ethereum\geth.ipc
Parity: ~\AppData\Local\Parity\Ethereum\jsonrpc.ipc

Look for this file and make sure it’s there before you get started. Some of these paths could change with updates.

Your Node.js client

There’s a few modules out there that deal with IPC, node-ipc and json-ipc-lib being two of them. Node already comes with net, and supposedly that’s what the more popular node-ipc uses under the hood anyway so that’s what we’ll use.

const net = require('net');
const os = require('os');
const client = net.createConnection({ path:os.homedir()+'/.local/share/io.parity.ethereum/jsonrpc.ipc'}, () => {
console.log('Connected to server...');
});

That connects us to the IPC server, which you can adapt to Geth. Certain Geth and Parity methods require parameters, but this one doesn’t so the list is empty.

let obj = {
jsonrpc : "2.0",
method : "web3_clientVersion",
params : [],
id : 01
};
client.write(JSON.stringify(obj),() => {
console.log('Call made.');
});

As you can see, the client.write function sends the data over to the server.

To listen for a server response:

client.on('data', (data) => {
console.log('Oh hey, some data: \n'+ data.toString());
client.end();
});

The client.end() is there to go ahead and close up the connection but you can put that were you see fit.

Don’t forget a disconnect listener:

client.on('end', () => {
console.log('Disconnected from server. Adios amigo!');
});

The result I get is:

Connected to server...
Call made
Oh hey, some data:
{"jsonrpc":"2.0","result":"Parity-Ethereum//v2.3.0-beta-10657d9-20190115/x86_64-linux-gnu/rustc1.31.1","id":01}
Disconnected from server. Adios amigo!

The whole code

const net = require('net');
const os = require('os');
const client = net.createConnection({
path:os.homedir()+'/.local/share/io.parity.ethereum/jsonrpc.ipc'}, () => {
console.log('Connected to server...');
});
let obj = {
jsonrpc : "2.0",
method : "web3_clientVersion",
params : [],
id : 01,
};
client.write(JSON.stringify(obj),() => {
console.log('Call made');
});
client.on('data', (data) => {
console.log('Oh hey, some data: \n'+ data.toString());
client.end();
});
client.on('end', () => {
console.log('Disconnected from server. Adios amigo!');
});

Conclusion

Going the IPC route to communicate with Geth or Parity is pretty straight forward and for small messages, you don’t need any external modules.

In this article you saw a simple implementation using the Node.jsnet module to make calls to the server using methods found in the Ethereum docs. You also learned how you can watch for events from the server and process the data received using callbacks.

--

--

Jorge Gonzalez Vallejo

Mobile Dev @ ClaimWizard & Freelancing with LiliaSoftware