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!