DEV Community

Cover image for How to communicate with the cardano node on a remote host
Srdjan Stankovic
Srdjan Stankovic

Posted on • Updated on

How to communicate with the cardano node on a remote host

Recently, while working on Cardano based NFT Marketplace, my team and I had to come up with a way for our backend to communicate with the cardano node on a remote host.

Given that our backend is deployed on AWS Fargate we had two choices:

  • Deploy cardano-node on AWS Fargate and somehow share UNIX socket file with our backend
  • Somehow expose UNIX socket file via some other protocol

We first tried to deploy the whole Cardano graphql stack (including cardano node) on AWS Fargate but this caused so much trouble for us so we decided to go try the other option.
While exploring for possible solutions we stumbled upon socat, a utility that lets you expose your UNIX socket via TCP (and much more honestly). If you are interested in finding more about socat here is an excellent post that I have personally used to come up with this solution.

Exposing Cardano Node socket over TCP

To expose cardano node UNIX socket file we first need to install the socat package. Installation will depend on your OS and package manager but for most major package manager socat should be available under the same name.
To install socat on Debian host (or any other OS using apt-get package manager) you can simply type:
sudo apt-get update # update packages
sudo apt-get install socat

Now when we have assured that we have required package (socat), to expose our cardano UNIX node socket path we should run following command:

socat TCP-LISTEN:3333,fork,reuseaddr, UNIX-CONNECT:$CARDANO_NODE_SOCKET_PATH
Enter fullscreen mode Exit fullscreen mode

This should start socat on localhost and expose our cardano node socket file, whose value is set in the CARDANO_NODE_SOCKET_PATH environment variable.
In case you don't have this environment variable set, you can do it by running:

export CARDANO_NODE_SOCKET_PATH='/path/to/cardano-node/node.socket'

Enter fullscreen mode Exit fullscreen mode

If you are running a cardano node with Docker you can checkout my repo where I have forked cardano-graphql repository and added a Docker container that exposes cardano node socket file via TCP. It's currently on v4.0.0 of cardano-graphql but I might update if there is enough interest for it.

Connecting to remote cardano node host via TCP

In order to connect to the cardano node on a remote host, we should run following command (given that we have socat already installed on our machine and exposed node socket on other):

socat UNIX-LISTEN:/path/to/local/node.socket,fork,reuseaddr,unlink-early, TCP:127.0.0.1:3333
Enter fullscreen mode Exit fullscreen mode

You should replace UNIX-LISTEN value to some path on our local machine where we're going to hold our node.socket file, and also replace TCP value with IP of your remote machine.
If we now set CARDANO_NODE_SOCKET_PATH to the destination on our local machine we should be able to use cardano cli without running cardano node on our local machine.

Conclusion

While we're close to smart contracts on the cardano blockchain we still need a way to communicate with our cardano node over the CLI. Our team at Cardano Blue has taken advantage of cardano-cli and made sure trading NFTs is possible even without smart contracts yet available but once they're out I am sure developing applications like this will be much easier.

Discussion (3)

Collapse
rssfed23 profile image
Rob Knight

This didn't quite work for me.
On the remote node I had to run "socat TCP-LISTEN:3333,reuseaddr,fork, UNIX-CLIENT:node0.socket" and on the client the same command as in the article.
If I used the one in the article I'd get connection refused on the client every time. Though it worth sharing incase anyone else comes across it :)

Collapse
pyropy profile image
Srdjan Stankovic Author

You're right, thanks for spotting the error in the article.

Collapse
cili93 profile image
Aleksandar Ilic

Awesome work, thanks for sharing!