Drizzle DAPP with Meteor backend

Sergei Sevriugin
Coinmonks
6 min readJun 22, 2018

--

For some blockchain projects we need to store information in old fashion way using application server. It can be a private client information or a BLOB that we can’t afford to store in Ethereum due to the gas cost. In this case we can use Distributed Data Protocol (DDP) that is solving a lot of problems that all developers are facing: querying a server-side database, sending the results down to the client, and then pushing changes to the client whenever anything changes in the database.

In this article we would like to show how to use Meteor backend that supports DDP in Truffle/Drizzle distributed application.

Truffle/Drizzle/Meteor Application Map

Let’s start from the DAPP side. Just unpack Drizzle box and we have running DAPP in few steps. Now we need to add a library that supports DDP protocol to our Drizzle/Truffle application. In our case we use Asteroid that is an isomorphic/universal javascript library which allows to connect to a Meteor backend from almost any JS environment. To listen our server we need to provide to an endpoint and create new Asteroid object.

Then we can subscribe to our collections using asteroid.subscribe function.

As our DAPP is already using a Redux store we can use it to record sate changes and update user interface if needed. For example, if a new record is added to our server collection we dispatch a new action to save the changes.

To follow Redux store rules we also need create a Reducer for our distributed collection.

and then add it to the combined reducer defined in our Drizzle box.

In our exampe we have HomeContainer that is wrapping our Home components and provides translation for the state to the component props. Now we need to add our distributed collection to the mapping to have access to our server data in React.

Plase note then we also adding mapDispatchToProps to provide Home component with ability to add a data to the collection. Then two mappings are connected to React component using drizzleConnect.

So, now we are ready to update Home component and add a logic for show our server data.

Plase note that due to DDP we have access to server collection in our React component as props parameter and if server collection is updated then our React component will be re-rended to provide user with refreshed data.

We have connected Drizzle/Truffle DAPP with Meteor backend using Asteroid javascript libraly but it’s only one task that we need to solve. Our next goal to update DAPP subscription based on smart-contract state. For this purpose we are going to use Drizzle ‘out of the box’ SimpleStorage smart-contact.

Let’s have a look now at our backend side and integrate smart-contract logic there. Thanks to Truffle framework we can use the same artefacts that we’ve used alredy in our DAPP. Need to say that the backend also have client part running under Meteor/React control. We are not going to make transactions on server part and will use web3 library here without any key storage. Backend client part can use Truffle library with Metamask. To update React component we use withTracker function together with Tracker.Dependency object. In smart contract wrapper class we have simple getters and setters with depend and changed functions.

Now inside withTracker loop if contract data is changed then component props will be updated and then React component will be re-rendered.

Now we are going to update backend server part and make publication depends on the smart contract current state. On server we use web3 library to listen SimpleStorage event.

Now we can adjust publication for the collection

So we are publishing only items with data equal to collectionFilter that is SimpleStorage data. The problem here that Meteor.publish is not re-publish collection if collectionFilter is changed. Only client can push server publish and makes Task.find re-run to provide fresh data. Thanks to Tracker Meteor re-run subscribe and push re-publish if subscribe parameter is changed. So, we can update backend client subscription to get re-publish works in the following way:

Now we need to go back to our DAPP to make subscription works also here. First of all we are going to get update if store state is changed due to the smart-contract transaction and then if we have new state unsubscribe to the collection and subscribe again using updated SimpleStorage data value.

Now our DAPP use SimpleStorage as a key to ‘open’ off-chain data for the client. Our backend part is listening blockchain events and as soon as a new transaction arrived open access for the client to the new content. When the client update subscription using Asteroid unsubscribe/subscribe the new content will be available in React interface.

The full code in our GitHub repository

--

--