CloudRetro - Open Source Cloud Gaming Service for Retro Games written in Go
Tags: go, opensource, showdev, githunt
Published: 2019-12-01 16:58:54

2019 is the Year of Cloud gaming with the multiple announcements from Google Stadia, Microsoft XCloud, Playstation Now... and recently Google Stadia released this 19th November. Looking back, I was so excited when I first read the news from Google Stadia. Cloud gaming is a very innovative idea and it will definitely become the next generation of not only Game but also other fields of computer science. My curiosity about this technology motivated me to make a POC of Cloud-Gaming for Retro Game.
{% github https://github.com/giongto35/cloud-game no-readme %}

## Video Demo
{% youtube GUBrJGAxZZg %}

# About Cloud Gaming technology
As we know about web-server, the more information backend can provide, the more flexible the frontend can render. Cloud Gaming **maximizes backend control** and **minimizes frontend logic** by running Game on server and streaming images/audio to users. In this way, clients are totally decoupled from heavy logic and no longer limited by hardware constraints. If we apply the same methodology to other heavy offline applications like an operating system, E.g run Microsoft Window on Google Chrome Browser like this

Behind Cloud gaming is an **art of extremely low latency streaming** to ensure user interaction is as smooth as possible. At first, I have a doubt on Google claim because I don't believe the streaming technology reached this advance yet. For that reason, I made this project to verify that claim and the result is above my expectation. In this project, I applied [WebRTC](https://github.com/pions/webrtc/tree/master/examples/sfu-minimal) with Video/Audio Compression (VP8/OPUS) to optimize package size and implemented a distributed [infrastructure](https://github.com/giongto35/cloud-game/wiki/Infrastructure) to enable horizontal scaling and optimal server pairing for better network latency. 

I picked Golang for my POC because I’m familiar with it the most, then it turned out to work perfectly in my case. Go concurrency design greatly helped when dealing with concurrency and event stream. For example, I can achieve CrowdPlay with just a few lines of code using Golang Fan-in Fan-out pattern.

# Feature
**Cloud gaming**: Game logic and storage is hosted on cloud service. Images and audio are streamed to users in the most optimal way.
**Cross-platform**: The game is run on web browser, the most universal built-in app. No console, plugin, external app or devices are needed. Chrome with the latest version is recommended for the game.
**Emulator agnostic**: The game can be played directly without any extra effort to set up the gaming emulator or platform. It is based on [Libretro library](https://libretro.com) and [ludo](https://github.com/libretro/ludo)
**Crowdplay**: Follow the idea of crowdplay (TwitchPlaysPokemon), multiple players can play the same game together by addressing the same deeplink. This is the feature Google Stadia promised couldn't deliver for the first release.
**Vertically scaled**: The infrastructure is designed to be able to scale under high traffic by adding more instances.
**Cloud storage**: the Game state is storing on online storage, so you can come back and continue playing your incomplete game.

# Demo
**Single play**: [**http://cloudretro.io**](cloudretro.io)
**Crowd play**: [**Pokemon Emerald**](http://cloudretro.io/?id=652e45d78d2b91cd%7CPokemon - Emerald Version %28U%29)
Arrow key + H (Help) + Z X C V + S (Save) + A (Load) + F (Full screen)
**Chrome** and **Chrome on Android** is recommended. It's not working on IPhone and some other explorers. In an ideal network condition, the Game should be run smoothly as in the Video. Because I only hosted the platform on limited servers in US East, US West, Eu, Singapore, you may experience some latency issues + connection problem. You can try hosting the service following the instruction on Github.

# Crowdplay
By clicking these deep-link, you can join the game directly and play it together with other people joining the same link.
- [Play Pokemon Emerald](http://cloudretro.io/?id=652e45d78d2b91cd%7CPokemon%20-%20Emerald%20Version%20%28U%29)
- [Harvest Moon](http://cloudretro.io/?id=3f7462269e976303___Harvest%20Moon%20-%20Back%20to%20Nature%20%28USA%29)
- [Mario](http://cloudretro.io/?id=1953c570fee1f9e4___Super%20Mario%20Bros)

And you can host the new game by yourself by accessing [cloudretro.io](cloudretro.io) and click "share" button to generate a deeplink to your current game.

*Disclaimer*: You may experience lagging when joining a room, it is because the room is in a different zone. In that case, you can create a new room.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/g7s3vn5hn5heiemyp68z.gif)
(*Two players are playing the Pokemon Emerald together*)

# How I implemented it in Golang
I had presentation in **GopherCon Vietnam 2019** about this topic, you can find the [**Video**](https://www.youtube.com/watch?v=tVQ269gzZcE&list=PLtTY3_zVDGOCLIKrfeCMHYoYVtDVopbTC&index=12
Slide) and [**Slide**](https://docs.google.com/presentation/d/1VFv8l2hVUINv2U36r7L11g4Oa_zVjHlr6MYymUbQXT4/edit#slide=id.g6fb5703b68_0_10) here. You can see my speaker note in the slide as well. 
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/2p6yie0so6al81jq1391.png)
(*GoCode Diagram from slide*)

Please check  [Github](https://github.com/giongto35/cloud-game) repo and Wiki for more details.

# Next in the series
[**Part 1: How I design a cloud gaming service**](https://dev.to/giongto35/cloud-gaming-the-art-of-extremely-latency-streaming-4777)
{% link https://dev.to/giongto35/cloud-gaming-the-art-of-extremely-latency-streaming-4777 %}

# Author
[Nguyen Huu Thanh](https://www.linkedin.com/in/huuthanhnguyen/)
[Tri Dang Minh](https://trich.im)

# If you like the project, please give me a **star** or contribute to [Github](https://github.com/giongto35/cloud-game)
{% github https://github.com/giongto35/cloud-game %}