Loom SDK for Developers: Using an Indexing Layer for Lightning-Fast DApp Performance

Matthew Campbell
Loom Network
Published in
5 min readMay 31, 2018
Indexing Mysql, Postgres, ElasticSearch

DelegateCall.com

People have been asking us how you build performant social media sites on the blockchain, like DelegateCall or Steemit.

Steemit is the original social network on the blockchain, which uses some functionalities from Reddit and Medium. When we approached DelegateCall, we learned a lot from Steemit and moved forward.

From a user’s perspective, one of the most important things to have is a fast and interactive front-end, with features like search, tags, and notifications. Each of these would be difficult, if not impossible, to do in Solidity on Ethereum.

So, this is what ultimately brought us to create a secondary off-chain indexing layer for DelegateCall.

Searching on a blockchain – DelegateCall interface

What is Indexing?

Indexing is storing data from the blockchain into a secondary database that is more performant. We take events from smart contracts, and then emit them to a queue. Then we have listeners to this queue that insert the events into various databases. That way, we can use the cached version of the data to display on webpages. In the future, we could even do things like store proofs in the secondary indexes, so that light blockchain clients can validate them.

Etherscan & Infura

The Ethereum mainnet also allows this, and Etherscan is the best example. It’s a site that indexes all of Ethereum’s transactions and blocks for anyone to view. When you use it, you aren’t querying the blockchain in realtime, but looking at a cache of it.

Etherscan interface

Infura is the second example. It’s a full Web3 API that caches the complete Ethereum blockchain. Lots of developers are using this on webpages to be able to read the blockchain without interacting directly with it.

Code Example: Emitting Events

Now, let’s jump right into the code.

Note: This article is not meant to be a tutorial, more of a high-level overview of the feature. So won’t be breaking down the code line by line.

If you’re looking for more of a tutorial, you can skip to our sample DApp below.

Emitting events from Go smart contracts:

emitMsg := struct {    
Owner string
Method string
Addr []byte }{owner, "createacct", addr}
emitMsgJSON, err := json.Marshal(emitMsg)
if err != nil {
log.Println("Error marshalling emit message")
}
ctx.Emit(emitMsgJSON)

Emitting events from Solidity:

Subscribing to Events

For subscribing to events and indexing them in another database, you can use WebSockets or a Redis queue.

Subscribing via WebSockets

The Loom SDK query endpoint can be used to subscribe to the event stream, as well. Here’s a sample subscription code using a command line and the wscat CLI:

wscat CLI

And another sample in Node.js with Web3 WebSockets:

Node.js eventing with WebSockets

We can also subscribe via a Redis queue.

By default, the loom-sdk will only emit events to the log. To configure it to send it to a Redis sorted set, add the following line to the loom.yaml config file.

EventDispatcherURI: "redis://localhost:6379"

This will start emitting events to the Redis server in a sorted set called loomevents. Each event is added to the sorted set with the score being the blockchain height.

Sample DApp: Social Networking Site

We built a sample social networking DApp, in which users can create comments and other users can comment on them.

Download the source code: Solitity Social App on GitHub

The comments are stored on a Loom DAppChain, and we have a Node.js backend that listens to the WebSocket and indexes the data into Elasticsearch.

Example social networking app on a DAppChain. Comments are written to the DAppChain, and also indexed in Elasticsearch for fast reads.

Note that the Node.js service and Elasticsearch index layer are off-chain, but all transactions are made directly to the DAppChain.

The Elasticsearch indexing layer is there as a supplementary service for lightning-fast reads, enabling a DApp that is as performant as any Web 2.0 site.

Go ahead and download the source code and check it out for yourself!

One more thing…

Our Loom SDK is so close to being ready! Plus a Hackathon coming to a city near you! 🌏

We have so many exciting things happening here at Loom Network, we can hardly contain ourselves!

In June, we will release our Loom SDK to the public. It will be the easiest way for developers to easily build and deploy scalable DApps and games, without having to do a lot of the heavy lifting.

We also have a number of Hackathons in the works (Beijing and Shanghai will happen in late June). At each of these events, we’ll be giving an exclusive introduction to the Loom SDK capabilities for both game and blockchain developers.

Our very first Hackathon in Tokyo was a great success, and we saw some really cool prototypes from super talented teams! Read about them here.

So, if you live and breathe code, we want to see you at one of these events! 😎

And if you want to help organize a Hackathon in your city, then get in touch with us at team@loomx.io!

We’ll talk soon, cool cat. 😎

Loom Network is the multichain interop platform for scaling high-performance dapps — already live in production, audited, and battle-tested.

Deploy your dapp to Loom’s Basechain once and reach the widest possible user base across all major blockchains today.

New to Loom? Start here.

Want to stake your LOOM tokens and help secure Basechain? Find out how.

Like what we’re doing here? Stay in the loop by signing up for our private mailing list.

--

--

Matthew Campbell
Loom Network

Cofounder https://loomx.io build scalable blockchain games and social apps on the Loom SDK.