Connect to the Ethereum Blockchain with Node.js and Vonage

Published April 24, 2020 by Caleb Ikhuohon

blog spotlight banner

Blockchain is a commonly used term that has many innovations associated with it. The blockchain is, quite literally, a chain of blocks, where each block is a piece of information stored in a public database, the chain. One such blockchain is Ethereum, which takes the concept of a blockchain to a whole new level.

Ethereum is a distributed ledger where users can conveniently agree upon code execution and data updates. The code being executed is a distributed application that contains logic to enable interaction between a user interface and data on the blockchain. A major facilitator for the interactions between the blockchain and a user interface are Events.

Smart contracts emit events from the blockchain which a user interface can listen for to enable specific actions and write event logs (data) to the blockchain. These logs can also be requested by the user interface.

In this tutorial, we’ll learn about blockchain events and how these events could be linked with the Vonage Conversation API to build real-world applications.

The code for this article is on GitHub.

Prerequisites

Create an Application

To get started with the Vongage Conversation API, you first need to create an application. This can be done through your Vonage APIs dashboard by clicking on the “Create a New Application” Button.

Create Vonage APIs Application

During the creation process, public and private keys are generated. The private key is automatically downloaded on your local machine and should be kept safe.

After the application has been created, you’ll be given an Application ID and API key to programmatically access the Vonage APIs Application, alongside the private key on our local machine.

Install Dependencies

The final step before we start writing code is to install our Node.js dependencies with NPM:

Next, set up a few NPM scripts to help run the application:

Set Up and Compile Solidity Contract

Solidity is a high-level programming language used to create smart contracts on the Ethereum Blockchain. In this tutorial, we’ll set up a smart contract to emit events from the Ethereum Blockchain once a message has been sent.

Next, we’ll compile this smart contract into a form that can be easily used by our Node.js application. This compilation is done using Truffle.

Once the compilation is done, a JSON file that describes the smart contract is generated in a build/contracts folder within the current directory.

Setup Node.js Application

To ensure the smooth execution of our application, we need a few environment variables set up in our application. These enable us to easily use the Vongage Conversation API to interact with our Ethereum node.

The Nexmo Application ID, API key, and APP Secret can be found on the application’s dashboard and should be stored in a .env file within the current directory. These constants are initialized with our Nexmo SDK instance.

The Vonage Conversation API provides a host of APIs to enables real-time communication over several channels (voice, text, video). Interactions via these channels can be initiated by creating a Conversation.

The conversation ID returned is used for emitting and listening for events that occur in this conversation.

Users are then created to interact in this conversation via membership. We also take note of the resulting Member ID.

Connect to the Ethereum Blockchain

To interact with our local Ethereum Node (a program which connects to the Ethereum network, provided by Ganache-cli in this case for testing purposes) through Node.js, we use Web3, a set of libraries which provides APIs for JavaScript and Node.js applications to connect to an Ethereum Node.

Here, we use a WebSocket provider that connects our Node.js application to the Ganache-cli RPC client. The Ganache-cli needs to be started after installation with the command:

Next, we pass information about the smart contract to our Node.js application and instantiate the contract. The Contract information is found in the ABI (Application Binary Interface) contained in the JSON file autogenerated from compiling our smart contract.

You may notice in the code snippet above, we need an account from which transactions would be sent. Ganache-cli provides a couple of test accounts and their associated private keys which are used throughout the development process.

Send a Message to the Network

Next, we send a message to the Ethereum Node and create a Conversation event once the transaction hash becomes available. The Conversation event contains the data (message sent) from the transaction.

Listen for Blockchain Events

We could also listen to the NewText event emitted from the Blockchain. Once this event is successful, we could emit a custom Conversation event which could be used by a user interface to listen for messages from the blockchain.

Put Everything Together

It’s now time to test our application! First, we ensure Ganache-cli is running in the terminal and our smart contract has been compiled with Truffle. Once these have been done, we run npm start in a new terminal instance to run our application.

A look at our Ganache-cli logs in the terminal should show the eth_sendTransaction and eth_subscribe APIs being called on our Ethereum Node, which indicates the start of a transaction and a subscription to an event by our Node.js application

What’s Next?

We could build on this application by building out a fully-fledged chat application on the Blockchain, including more robust error handling, indexing messages on the blockchain, and much more!

Leave a Reply

Your email address will not be published.

Get the latest posts from Nexmo’s next-generation communications blog delivered to your inbox.

By signing up to our communications blog, you accept our privacy policy , which sets out how we use your data and the rights you have in respect of your data. You can opt out of receiving our updates by clicking the unsubscribe link in the email or by emailing us at [email protected].