Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
2016 - Ramesh Sampath - Building a Tic-Tac-Toe ...
Search
PyBay
September 25, 2016
0
68
2016 - Ramesh Sampath - Building a Tic-Tac-Toe Two-Player Game using Tornado over Websockets
PyBay
September 25, 2016
Tweet
Share
More Decks by PyBay
See All by PyBay
2017 - The Packaging Gradient
pybay
2
840
2017 - Building Bridges: Stopping Python 2 without damages
pybay
0
590
2017 - Bringing Python 3 to LinkedIn
pybay
1
520
2017 - Python Debugging with PUDB
pybay
0
580
2017 - Opening up to Open Source
pybay
0
220
2017 - A Gentle Introduction to Text Classification with Deep Learning
pybay
2
160
2017 - Performant Asynchronous Programming at Quora
pybay
1
330
2017 - latus - a Personal Cloud Storage App written in Python
pybay
2
460
2017 - Everything You Ever Wanted to Know About Web Authentication in Python
pybay
3
510
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Designing the Hi-DPI Web
ddemaree
280
34k
The Invisible Side of Design
smashingmag
298
50k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Building Your Own Lightsaber
phodgson
103
6.1k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
The Language of Interfaces
destraynor
154
24k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Transcript
Tic-Tac-Toe App with Tornado & WebSockets By Ramesh Sampath APP:
http://games.sampathweb.com
Ramesh Sampath • Engineer ◦ Build Applications ◦ Build Machine
Learning models ◦ Deploy @sampathweb (github / twitter / linkedin)
Objective Build and Deploy a Tic-Tac-Toe App that we can
play with Friends.
Audience • New to WebSockets • May have built some
Webapps • Want to built a Game / Chat App
Agenda • Tic-Tac-Toe - Single Player • Two Player Game
• WebSockets • Tornado • Deploy • Scaling
Tic-Tac-Toe App (Command Line)
Tic Tac Toe Source: Wikipedia
Demo Command Line Version
Tic-Tac-Toe - Play with Human
Two Player Version Web Server Want to Play Other
Web App Clients Web Server Request Response Web App (Python)
Web App • Request / Response • Scale to lots
of connections ◦ Short lived ◦ Isolated • Flask / Django over WSGI / Gunicorn & Nginx
Two Player Version Web Server Want to Play Other
Web App • Ajax Long Polling ◦ Latency ◦ Polling
Frequency ◦ Server cannot initiate Push • Blocking - One Request at a Time.
WebSockets
WebSockets WebSocket is a protocol providing full-duplex communication channels over
a single TCP connection. (RFC 6455)
WebSockets • Tunnel Data (String, Blob, ArrayBuffer) ◦ JSON ◦
XML / HTML ◦ Images, Sound, Video
WebSockets API • Open ◦ var ws = new WebSocket(ws://<endpoint>)
• Send Message ◦ ws.send() • Receive Message ◦ ws.onmessage()
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
WebSockets • Persistent Connection • Two-way communication • Handle Lots
of Connections • Non-Blocking (Async)
Async Stack Credits: Anton Caceres - Better asynchronous code with
Tornado and Python 3 [EuroPython 2015]
Tornado • Web Framework • Single Threaded • Non-Blocking I/O
Concurrency • Thousands of Requests
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
Demo!
Code Walkthrough Open Text Editor
Things to Lookout For • Async entire code ◦ Database
Calls - Momoko over SqlAlchemy ◦ External HTTP Calls - AsyncHTTP ◦ Non Async Libraries ▪ Wrapped in coroutines / Threadpool
Deploy to AWS
Deploy Steps • Deploy Notes in the “README.md” file of
repo. https://github.com/sampathweb/board-games-app
Deployment Handler Application S T A T E Handler Handler
Handler Supervisor Process
Scaling App Handler Application S T A T E Handler
Handler Handler Handler Application S T A T E Handler Handler Handler Not Connected
Problems • Single Instance of App ◦ Latency ◦ Number
of connections • Source: Singleton Manager to maintain State
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)
Thank You! Slides: https://goo.gl/NKzuZB @sampathweb (Github / Twitter / Linkedin)
WebSockets Web Server Web App (Python) Client Client Client Client
Starts with HTTP Handshake