Upgrade to Pro — share decks privately, control downloads, hide ads and more …

2016 - Ramesh Sampath - Building a Tic-Tac-Toe ...

PyBay
August 20, 2016

2016 - Ramesh Sampath - Building a Tic-Tac-Toe Two-Player Game using Tornado over Websockets

Description
Learn how to build a Two-player game using Python Tornado web framework. We will be using websockets to make the app realtime.

Abstract
We will live code and learn how to build a real-time game app using Tornado web framework and websockets. Through this app, we will learn how to write an web app using Tornado web framework (http://www.tornadoweb.org/), and how to communicate over websockets. We will be building a Tic-Tac-Toe two-player game to learn about these concepts. A player can start a new game or accept a challenge from another player.

When a player starts a new game, the app would create a new game channel, provide a handle to the channel that the player can send to his friend to join in and play. We will not be dealing with any authentication or logins to start a new game or to join an existing one. The goal is to show how easy it's to build an realtime app with Tornado.

Bio
Ramesh loves building data products that blend visualization and machine learning. He mostly uses Flask / Tornado to create web apps, Pandas / Scikit-learn for build machine learning models and D3 for visualizations

https://youtu.be/SkETonolR3U

PyBay

August 20, 2016
Tweet

More Decks by PyBay

Other Decks in Programming

Transcript

  1. Ramesh Sampath • Engineer ◦ Build Applications ◦ Build Machine

    Learning models ◦ Deploy @sampathweb (github / twitter / linkedin)
  2. Audience • New to WebSockets • May have built some

    Webapps • Want to built a Game / Chat App
  3. Agenda • Tic-Tac-Toe - Single Player • Two Player Game

    • WebSockets • Tornado • Deploy • Scaling
  4. Web App • Request / Response • Scale to lots

    of connections ◦ Short lived ◦ Isolated • Flask / Django over WSGI / Gunicorn & Nginx
  5. Web App • Ajax Long Polling ◦ Latency ◦ Polling

    Frequency ◦ Server cannot initiate Push • Blocking - One Request at a Time.
  6. WebSockets API • Open ◦ var ws = new WebSocket(ws://<endpoint>)

    • Send Message ◦ ws.send() • Receive Message ◦ ws.onmessage()
  7. WebSockets Handler Open Socket Connection • Send Message • Receive

    Messages • Close Connection Handler Handler Handler Handler Application G A M E S T A T E
  8. Tornado - When to use it • Building REST APIs

    (GET / POST) • Micro Services • Slow database queries • Communicating with external resources • Need to handle large number of connections
  9. Things to Lookout For • Async entire code ◦ Database

    Calls - Momoko over SqlAlchemy ◦ External HTTP Calls - AsyncHTTP ◦ Non Async Libraries ▪ Wrapped in coroutines / Threadpool
  10. Deploy Steps • Deploy Notes in the “README.md” file of

    repo. https://github.com/sampathweb/board-games-app
  11. Scaling App Handler Application S T A T E Handler

    Handler Handler Handler Application S T A T E Handler Handler Handler Not Connected
  12. Problems • Single Instance of App ◦ Latency ◦ Number

    of connections • Source: Singleton Manager to maintain State
  13. Scale with Pub/Sub Load Balancer (Nginx) Clients Tornado Process #1

    Tornado Process #2 Tornado Process #3 Tornado Process #4 Supervisor Process Pub / Sub (Redis)