There are lots of different solutions for creating decentralised applications that scale to thousands or even millions of real-time users such as plasma and state channels. In this guide you’ll learn step-by-step how state channels work and how to create scalable applications in Ethereum right now.

You don’t have to wait for future improvements in the blockchain. The technology is here to stay and you can use it for creating all kinds of dApps. Right now, state channels are mostly used for blockchain-based games in Ethereum.

Think gambling with crypto currencies. There are ways to encrypt the information to keep reveal it later which is a key point of this system.

What are state channels?

It’s a scaling solution to create decentralized applications and smart contracts that can be used by millions of users in almost real-time. They work by initiating a channel between 2 or more users in which they exchange encrypted, signed messages with the information of the transaction they want to execute.

They are called “state” because each interaction has to have a state that can be updated. Think about a score of a game or a bank balance.

Why do they exist?

State channels were created because ethereum applications quickly grew in popularity making the blockchain unusable since it was developed with a moderate use. They allow continuous transactions without paying for gas or waiting for miners to process the transactions.

Which means free and fast transactions.

What do we need to set up a state channel?

At least 2 users that will interact with each other. A channel needs to be opened between 2 or more users. Similar to a chat application. A smart contract with the state channel logic that will open and close it. If the state channel will be used in a game, an escrow will be required for both users. That escrow in ether will be stored in the smart contract when opening the channel. A javascript application that will generate the signed messages that will be exchanged off-chain between the users. Metamask or a similar tool for signing messages. Signing messages don’t cost gas and they are executed instantly. It’s required from both users to sign the messages to guarantee that tehy are the ones generating such transaction. Email or any external application to exchange those signed messages to make that application possible.

How do they work?

State channel are a bit complex to set up because you have to make sure both players protected in case anything goes wrong, that’s why we need a smart contract. These are the steps:

In a state channel between 2 users, the first one deploys the smart contract which will “open” the channel. The second one executes a function of that smart contract to “join” that state channel” Then they can begin exchanging signed messages for the application. Both users have access to a custom javascript application to generate messages with the information that they would do in a smart contract, but off-chain. The speed of the transactions depend on how fast each user can create and sign those messages. They keep exchanging messages, playing off-chain until they decide that the game is over. When they finish the game, anyone of them can go to the smart contract and execute a function to finish it which will start the phase of “negotiation”. In this phase, both users have a timeout of 1 day to upload the latest 2 messages they have to the smart contract. The smart contract checks the latest messages and releases the funds to finish the game based on that information. Each message contains the results of the previous interactions so it’s safe to just check the latest ones.

How can you apply this in a real-world situation?

In this guide I’m going to show you how to create a state channel between 2 users for an Ethereum game. Remember that state channels can be used for any kind of application that has a “state” or a counter. That’s why games are ideal. Because you can track who is winning each game, there’s a state for each game that can be updated.

We’ll create a dice game where player 1 chooses the number of the dice that will come off and player 2 has to guess that number to win. They will be able to play as many games as they want without having to execute transactions on the blockchain. We will also have a web app to show the interface.

This is the index that we’ll follow to create such decentralised application: