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

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

PyBay
September 25, 2016
68

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

PyBay

September 25, 2016
Tweet

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)