SlideShare una empresa de Scribd logo
1 de 45
Descargar para leer sin conexión
shiroyuki on twitter and github http://www.shiroyuki.com 
WebSocket 101 in Python 
PyCon Japan 2014! 
September 14, 2014 
15:35 - 16:05 JST 
Tokyo, Japan 
Juti Noppornpitak! 
Senior Software Developer 
Instaclick Inc. 
Toronto, Canada 
1
Juti Noppornpitak This Session’s Speaker 
2
What’s on the menu? 
3
What is WebSocket? 
4
WebSocket 
• A protocol provides full-duplex communication 
channels between processes (or machines). 
• It is standardized as RFC 6455 in 2011. 
• It is design for web development. 
• It utilizes HTTP for the handshake process. This 
means WebSocket apps can operate on normal HTTP 
ports (80 or 443). 
• Supported in all modern browsers. 
5
HTTP Server 
WS Server 
Browser 
HTTP 
WebSocket! 
Protocol 
6
Why develop a WebSocket server in Python? 
7
Why develop a WebSocket server in Python? 
• Lots of third-party libraries that we can use are 
available and mature. 
• Easily integrate with any home-grown modules. 
• Simplify the deployment as the whole app can 
be shipped in a single package. 
• We have full control on how it works and how it 
integrates with other components and services. 
8
Now, it is the time for the demo. 
9
What do we use to make a demo app? 
10
http ws 
Flask Tornado 
RabbitMQ 
Vanilla JavaScript 
jQuery 
SCSS 
Flexbox 
Pika 
11
Now, let’s see WebSocket in action! 
12
I know that some of you might want to see the 
code I use for the live demonstration. 
13
It is on GitHub. (> <) 
https://github.com/shiroyuki/pyconjp-2014-ws-demo 
! 
Sorry, BitBucket sport fans. I am too lazy 
to mirror the code on BitBucket. 
14
This demo is only compatible with Python 2.7.5 or older. 
Or use my patch for Pika if you want to use with Python 2.7.6 or newer. 
If you try on OS X 10.9, there should be no problem. 
! 
The fork of Pika with the patch to accommodate 
Python ticket 8865 is available https://github.com/shiroyuki/pika.
Step 0 - Mockup 
Before we start, let’s see what the UI looks like. 
(Branch: step-0-mockup) 
16
Demo 
17
Step 1 - Basic WebSocket Integration 
We start the basic integration with WebSocket. 
(Branch: step-1-basic-ws) 
18
19
Demo 
20
What just happened? 
21
A handshake with HTTP from the client 
this.client = new WebSocket("ws://localhost:8080/relay"); 
GET ws://localhost:8080/relay HTTP/1.1 
Host: localhost:8080 
Upgrade: websocket 
Connection: Upgrade 
Origin: http://localhost:8000 
... 
22
A handshake with HTTP from the server 
HTTP/1.1 101 Switching Protocols 
Upgrade: websocket 
Connection: Upgrade 
... 
The client (browser) switches to use the WebSocket protocol. 
23
How do the client and the server 
talk to each other at this step? 
24
Step 2 - Chat app without RabbitMQ 
We start making it work without RabbitMQ. 
(Branch: step-2-work-without-rabbitmq) 
25
The features of the demo chat app 
• No registration needed. Just say who you are 
every time you load the interface. 
• It is like an Internet Relay Chat app. The 
difference is that the app does not use the IRC 
protocol. 
• All identities must be unique. 
• The identification is needed before users can 
send or receive messages. 
26
Identification 
Process 
This diagram shows the procedure 
of user identification which is similar 
to the process of notifying when a 
user leaves the chat room. 
27
Message Exchange 
28
Demo 
You may now join the conversation at 
http://home.shiroyuki.com:8000/. 
Please be nice to each other. 
29
The differences from the previous step 
• Git Diff: https://github.com/shiroyuki/ 
pyconjp-2014-ws-demo/compare/step-1-basic-ws... 
step-2-work-without-rabbitmq 
• The communication is changed from raw string 
to JSON-RPC, which is more flexible. 
• The Tornado app (ws.py) has the ability to 
broadcast messages to other clients. 
30
Step 3 - Work with RabbitMQ 
Now, we make it work with RabbitMQ. 
(Branch: step-3-work-with-rabbitmq) 
31
Has anyone missed Pika? 
32
Don’t worry. We are using it 
in this step with RabbitMQ. 
33
How do we use Pika for this application? 
• Design to use one blocking connection, which 
is the simplest setup for AMQP with Pika. 
• Use only one durable fan-out exchange, called 
demo-chat, which is automatically declared if 
the exchange does not exists. 
• Each socket connection has its own temporary 
queue (only one) bound with the demo-chat 
exchange. 
34
Necessary changes to make 
• The procedures to consume and publish 
messages are asynchronous. Because we are 
using a blocking connection which can block the 
event loop of the main thread (the Tornado app). 
• This app is now multi-threading. 
• RabbitMQ is used only for broadcasting 
messages to multiple users. 
35
Comparing with the previous step… 
• Cons: The nature of multi-threading applications 
leads to complexity and potential freeze. 
• Pros: Scalability as RabbitMQ handles the 
message queues. 
• Pros: Slightly increase the turnover rate in the 
communication between the client and the 
server. 
36
Demo 
For the last time… Trust me. 
37
As you can see, from the user perspective, 
nothing changes. 
38
Only the method of 
exchanging messages! 
are different. 
39
Just to be clear, this demo app may not 
use libraries properly and efficiently. 
40
What more can you improve the demo app? 
• Off-load the Tornado app by letting the Flask app 
asynchronously publish the message in order 
to increase the message turnover rate in Tornado 
app. 
• Use pika.adapters.TornadoConnection for 
proper integration with Tornado Framework.(http:// 
pika.readthedocs.org/en/latest/examples/ 
tornado_consumer.html) 
• and many more. 
41
And, no more demo 
Otherwise, this will be WebSocket 102. 
42
Alternatives? 
flask-sockets 
gevent-websocket 
flask-socketio 
autobahn 
your imagination 
43
The end 
最後まで聞いてくれて、どうもありがとうございました。 
44 Taken in Montréal, Quebec, Canada on April, 2014
All photographs are taken and copyrighted by Juti Noppornpitak.

Más contenido relacionado

La actualidad más candente

Performance Comparison of PHP 5.6 vs. 7.0 vs HHVM
Performance Comparison of PHP 5.6 vs. 7.0 vs HHVMPerformance Comparison of PHP 5.6 vs. 7.0 vs HHVM
Performance Comparison of PHP 5.6 vs. 7.0 vs HHVMJani Tarvainen
 
A look at FastCgi & Mod_PHP architecture
A look at FastCgi & Mod_PHP architectureA look at FastCgi & Mod_PHP architecture
A look at FastCgi & Mod_PHP architectureAimee Maree Forsstrom
 
PHP and FastCGI Performance Optimizations
PHP and FastCGI Performance OptimizationsPHP and FastCGI Performance Optimizations
PHP and FastCGI Performance OptimizationsAlessandro Pilotti
 
Liberating the Black Box - Real-Time Communications for the Internet of Things
Liberating the Black Box - Real-Time Communications for the Internet of ThingsLiberating the Black Box - Real-Time Communications for the Internet of Things
Liberating the Black Box - Real-Time Communications for the Internet of ThingsPeter Moskovits
 
Smuggling TCP traffic through HTTP
Smuggling TCP traffic through HTTPSmuggling TCP traffic through HTTP
Smuggling TCP traffic through HTTPDávid Halász
 
Building Desktop RIAs with JavaScript and PHP - ZendCon09
Building Desktop RIAs with JavaScript and PHP - ZendCon09Building Desktop RIAs with JavaScript and PHP - ZendCon09
Building Desktop RIAs with JavaScript and PHP - ZendCon09funkatron
 
Zendcon magento101
Zendcon magento101Zendcon magento101
Zendcon magento101Mathew Beane
 
Php Performance On Windows
Php Performance On WindowsPhp Performance On Windows
Php Performance On Windowsruslany
 
Websockets at tossug
Websockets at tossugWebsockets at tossug
Websockets at tossugclkao
 
PHP Enhancement with Windows Server 2008
PHP Enhancement with Windows Server 2008PHP Enhancement with Windows Server 2008
PHP Enhancement with Windows Server 2008Krit Kamtuo
 
Word press workflows and gulp
Word press workflows and gulpWord press workflows and gulp
Word press workflows and gulpEli McMakin
 
maXbox starter46 work with Wine
maXbox starter46 work with WinemaXbox starter46 work with Wine
maXbox starter46 work with WineMax Kleiner
 
Last Month in PHP - March 2016
Last Month in PHP - March 2016Last Month in PHP - March 2016
Last Month in PHP - March 2016Eric Poe
 
Kamailio World 2014 - Introduction to IMS Application Servers
Kamailio World 2014 - Introduction to IMS Application ServersKamailio World 2014 - Introduction to IMS Application Servers
Kamailio World 2014 - Introduction to IMS Application Serverscaruizdiaz
 
Real time web: is there a life without socket.io and node.js?
Real time web: is there a life without socket.io and node.js?Real time web: is there a life without socket.io and node.js?
Real time web: is there a life without socket.io and node.js?Eduard Trayan
 
Message Queues & Offline Processing with PHP
Message Queues & Offline Processing with PHPMessage Queues & Offline Processing with PHP
Message Queues & Offline Processing with PHPmarcelesser
 

La actualidad más candente (20)

Performance Comparison of PHP 5.6 vs. 7.0 vs HHVM
Performance Comparison of PHP 5.6 vs. 7.0 vs HHVMPerformance Comparison of PHP 5.6 vs. 7.0 vs HHVM
Performance Comparison of PHP 5.6 vs. 7.0 vs HHVM
 
A look at FastCgi & Mod_PHP architecture
A look at FastCgi & Mod_PHP architectureA look at FastCgi & Mod_PHP architecture
A look at FastCgi & Mod_PHP architecture
 
PHP and FastCGI Performance Optimizations
PHP and FastCGI Performance OptimizationsPHP and FastCGI Performance Optimizations
PHP and FastCGI Performance Optimizations
 
SPDY - or maybe HTTP2.0
SPDY - or maybe HTTP2.0SPDY - or maybe HTTP2.0
SPDY - or maybe HTTP2.0
 
Liberating the Black Box - Real-Time Communications for the Internet of Things
Liberating the Black Box - Real-Time Communications for the Internet of ThingsLiberating the Black Box - Real-Time Communications for the Internet of Things
Liberating the Black Box - Real-Time Communications for the Internet of Things
 
Smuggling TCP traffic through HTTP
Smuggling TCP traffic through HTTPSmuggling TCP traffic through HTTP
Smuggling TCP traffic through HTTP
 
PHP and node.js Together
PHP and node.js TogetherPHP and node.js Together
PHP and node.js Together
 
Building Desktop RIAs with JavaScript and PHP - ZendCon09
Building Desktop RIAs with JavaScript and PHP - ZendCon09Building Desktop RIAs with JavaScript and PHP - ZendCon09
Building Desktop RIAs with JavaScript and PHP - ZendCon09
 
Zendcon magento101
Zendcon magento101Zendcon magento101
Zendcon magento101
 
Php Performance On Windows
Php Performance On WindowsPhp Performance On Windows
Php Performance On Windows
 
Phalcon 2 - PHP Brazil Conference
Phalcon 2 - PHP Brazil ConferencePhalcon 2 - PHP Brazil Conference
Phalcon 2 - PHP Brazil Conference
 
Websockets at tossug
Websockets at tossugWebsockets at tossug
Websockets at tossug
 
PHP Enhancement with Windows Server 2008
PHP Enhancement with Windows Server 2008PHP Enhancement with Windows Server 2008
PHP Enhancement with Windows Server 2008
 
Word press workflows and gulp
Word press workflows and gulpWord press workflows and gulp
Word press workflows and gulp
 
maXbox starter46 work with Wine
maXbox starter46 work with WinemaXbox starter46 work with Wine
maXbox starter46 work with Wine
 
Last Month in PHP - March 2016
Last Month in PHP - March 2016Last Month in PHP - March 2016
Last Month in PHP - March 2016
 
Kamailio World 2014 - Introduction to IMS Application Servers
Kamailio World 2014 - Introduction to IMS Application ServersKamailio World 2014 - Introduction to IMS Application Servers
Kamailio World 2014 - Introduction to IMS Application Servers
 
Realtime with websockets
Realtime with websocketsRealtime with websockets
Realtime with websockets
 
Real time web: is there a life without socket.io and node.js?
Real time web: is there a life without socket.io and node.js?Real time web: is there a life without socket.io and node.js?
Real time web: is there a life without socket.io and node.js?
 
Message Queues & Offline Processing with PHP
Message Queues & Offline Processing with PHPMessage Queues & Offline Processing with PHP
Message Queues & Offline Processing with PHP
 

Similar a Websocket 101 in Python

RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...
RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...
RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...Datacratic
 
Workshop For pycon13
Workshop For pycon13Workshop For pycon13
Workshop For pycon13Steven Pousty
 
Open MPI SC'15 State of the Union BOF
Open MPI SC'15 State of the Union BOFOpen MPI SC'15 State of the Union BOF
Open MPI SC'15 State of the Union BOFJeff Squyres
 
Real Time Realitites
Real Time RealititesReal Time Realitites
Real Time Realititesmarkisuak
 
ONLINE FOOD ORDERS THROUGH WHATSAPP AUTOMATION BOT
ONLINE FOOD ORDERS THROUGH WHATSAPP AUTOMATION BOTONLINE FOOD ORDERS THROUGH WHATSAPP AUTOMATION BOT
ONLINE FOOD ORDERS THROUGH WHATSAPP AUTOMATION BOTIRJET Journal
 
Increasing velocity via serless semantics
Increasing velocity via serless semanticsIncreasing velocity via serless semantics
Increasing velocity via serless semanticsKfir Bloch
 
substrate: A framework to efficiently build blockchains
substrate: A framework to efficiently build blockchainssubstrate: A framework to efficiently build blockchains
substrate: A framework to efficiently build blockchainsservicesNitor
 
WebRTC: A front-end perspective
WebRTC: A front-end perspectiveWebRTC: A front-end perspective
WebRTC: A front-end perspectiveshwetank
 
Возможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSВозможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSCisco Russia
 
Python for IoT, A return of experience
Python for IoT, A return of experiencePython for IoT, A return of experience
Python for IoT, A return of experienceAlexandre Abadie
 
Using Python for IoT: a return of experience, Alexandre Abadie
Using Python for IoT: a return of experience, Alexandre AbadieUsing Python for IoT: a return of experience, Alexandre Abadie
Using Python for IoT: a return of experience, Alexandre AbadiePôle Systematic Paris-Region
 
What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...
What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...
What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...Heiko Voigt
 
Free Mongo on OpenShift
Free Mongo on OpenShiftFree Mongo on OpenShift
Free Mongo on OpenShiftSteven Pousty
 

Similar a Websocket 101 in Python (20)

RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...
RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...
RTBkit Meetup - Developer Spotlight, Behind the Scenes of RTBkit and Intro to...
 
Monkey Server
Monkey ServerMonkey Server
Monkey Server
 
Workshop For pycon13
Workshop For pycon13Workshop For pycon13
Workshop For pycon13
 
Open MPI SC'15 State of the Union BOF
Open MPI SC'15 State of the Union BOFOpen MPI SC'15 State of the Union BOF
Open MPI SC'15 State of the Union BOF
 
VozDigital DevFest 31/10/14
VozDigital DevFest 31/10/14VozDigital DevFest 31/10/14
VozDigital DevFest 31/10/14
 
Real Time Realitites
Real Time RealititesReal Time Realitites
Real Time Realitites
 
ClueCon 2017
ClueCon 2017ClueCon 2017
ClueCon 2017
 
ONLINE FOOD ORDERS THROUGH WHATSAPP AUTOMATION BOT
ONLINE FOOD ORDERS THROUGH WHATSAPP AUTOMATION BOTONLINE FOOD ORDERS THROUGH WHATSAPP AUTOMATION BOT
ONLINE FOOD ORDERS THROUGH WHATSAPP AUTOMATION BOT
 
DevCon5 (July 2014) - Intro to WebRTC
DevCon5 (July 2014) - Intro to WebRTCDevCon5 (July 2014) - Intro to WebRTC
DevCon5 (July 2014) - Intro to WebRTC
 
WebRTC Summit (June 2014) - WebRTC Interoperability (and why it is important)
WebRTC Summit (June 2014) - WebRTC Interoperability (and why it is important)WebRTC Summit (June 2014) - WebRTC Interoperability (and why it is important)
WebRTC Summit (June 2014) - WebRTC Interoperability (and why it is important)
 
Increasing velocity via serless semantics
Increasing velocity via serless semanticsIncreasing velocity via serless semantics
Increasing velocity via serless semantics
 
substrate: A framework to efficiently build blockchains
substrate: A framework to efficiently build blockchainssubstrate: A framework to efficiently build blockchains
substrate: A framework to efficiently build blockchains
 
Building APIs with Mule and Spring Boot
Building APIs with Mule and Spring BootBuilding APIs with Mule and Spring Boot
Building APIs with Mule and Spring Boot
 
WebRTC: A front-end perspective
WebRTC: A front-end perspectiveWebRTC: A front-end perspective
WebRTC: A front-end perspective
 
Возможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSВозможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OS
 
Webrtc in Real world
Webrtc in Real world Webrtc in Real world
Webrtc in Real world
 
Python for IoT, A return of experience
Python for IoT, A return of experiencePython for IoT, A return of experience
Python for IoT, A return of experience
 
Using Python for IoT: a return of experience, Alexandre Abadie
Using Python for IoT: a return of experience, Alexandre AbadieUsing Python for IoT: a return of experience, Alexandre Abadie
Using Python for IoT: a return of experience, Alexandre Abadie
 
What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...
What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...
What is cool with Domino V10, Proton and Node.JS, and why would I use it in ...
 
Free Mongo on OpenShift
Free Mongo on OpenShiftFree Mongo on OpenShift
Free Mongo on OpenShift
 

Último

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 

Último (20)

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 

Websocket 101 in Python

  • 1. shiroyuki on twitter and github http://www.shiroyuki.com WebSocket 101 in Python PyCon Japan 2014! September 14, 2014 15:35 - 16:05 JST Tokyo, Japan Juti Noppornpitak! Senior Software Developer Instaclick Inc. Toronto, Canada 1
  • 2. Juti Noppornpitak This Session’s Speaker 2
  • 3. What’s on the menu? 3
  • 5. WebSocket • A protocol provides full-duplex communication channels between processes (or machines). • It is standardized as RFC 6455 in 2011. • It is design for web development. • It utilizes HTTP for the handshake process. This means WebSocket apps can operate on normal HTTP ports (80 or 443). • Supported in all modern browsers. 5
  • 6. HTTP Server WS Server Browser HTTP WebSocket! Protocol 6
  • 7. Why develop a WebSocket server in Python? 7
  • 8. Why develop a WebSocket server in Python? • Lots of third-party libraries that we can use are available and mature. • Easily integrate with any home-grown modules. • Simplify the deployment as the whole app can be shipped in a single package. • We have full control on how it works and how it integrates with other components and services. 8
  • 9. Now, it is the time for the demo. 9
  • 10. What do we use to make a demo app? 10
  • 11. http ws Flask Tornado RabbitMQ Vanilla JavaScript jQuery SCSS Flexbox Pika 11
  • 12. Now, let’s see WebSocket in action! 12
  • 13. I know that some of you might want to see the code I use for the live demonstration. 13
  • 14. It is on GitHub. (> <) https://github.com/shiroyuki/pyconjp-2014-ws-demo ! Sorry, BitBucket sport fans. I am too lazy to mirror the code on BitBucket. 14
  • 15. This demo is only compatible with Python 2.7.5 or older. Or use my patch for Pika if you want to use with Python 2.7.6 or newer. If you try on OS X 10.9, there should be no problem. ! The fork of Pika with the patch to accommodate Python ticket 8865 is available https://github.com/shiroyuki/pika.
  • 16. Step 0 - Mockup Before we start, let’s see what the UI looks like. (Branch: step-0-mockup) 16
  • 18. Step 1 - Basic WebSocket Integration We start the basic integration with WebSocket. (Branch: step-1-basic-ws) 18
  • 19. 19
  • 22. A handshake with HTTP from the client this.client = new WebSocket("ws://localhost:8080/relay"); GET ws://localhost:8080/relay HTTP/1.1 Host: localhost:8080 Upgrade: websocket Connection: Upgrade Origin: http://localhost:8000 ... 22
  • 23. A handshake with HTTP from the server HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade ... The client (browser) switches to use the WebSocket protocol. 23
  • 24. How do the client and the server talk to each other at this step? 24
  • 25. Step 2 - Chat app without RabbitMQ We start making it work without RabbitMQ. (Branch: step-2-work-without-rabbitmq) 25
  • 26. The features of the demo chat app • No registration needed. Just say who you are every time you load the interface. • It is like an Internet Relay Chat app. The difference is that the app does not use the IRC protocol. • All identities must be unique. • The identification is needed before users can send or receive messages. 26
  • 27. Identification Process This diagram shows the procedure of user identification which is similar to the process of notifying when a user leaves the chat room. 27
  • 29. Demo You may now join the conversation at http://home.shiroyuki.com:8000/. Please be nice to each other. 29
  • 30. The differences from the previous step • Git Diff: https://github.com/shiroyuki/ pyconjp-2014-ws-demo/compare/step-1-basic-ws... step-2-work-without-rabbitmq • The communication is changed from raw string to JSON-RPC, which is more flexible. • The Tornado app (ws.py) has the ability to broadcast messages to other clients. 30
  • 31. Step 3 - Work with RabbitMQ Now, we make it work with RabbitMQ. (Branch: step-3-work-with-rabbitmq) 31
  • 32. Has anyone missed Pika? 32
  • 33. Don’t worry. We are using it in this step with RabbitMQ. 33
  • 34. How do we use Pika for this application? • Design to use one blocking connection, which is the simplest setup for AMQP with Pika. • Use only one durable fan-out exchange, called demo-chat, which is automatically declared if the exchange does not exists. • Each socket connection has its own temporary queue (only one) bound with the demo-chat exchange. 34
  • 35. Necessary changes to make • The procedures to consume and publish messages are asynchronous. Because we are using a blocking connection which can block the event loop of the main thread (the Tornado app). • This app is now multi-threading. • RabbitMQ is used only for broadcasting messages to multiple users. 35
  • 36. Comparing with the previous step… • Cons: The nature of multi-threading applications leads to complexity and potential freeze. • Pros: Scalability as RabbitMQ handles the message queues. • Pros: Slightly increase the turnover rate in the communication between the client and the server. 36
  • 37. Demo For the last time… Trust me. 37
  • 38. As you can see, from the user perspective, nothing changes. 38
  • 39. Only the method of exchanging messages! are different. 39
  • 40. Just to be clear, this demo app may not use libraries properly and efficiently. 40
  • 41. What more can you improve the demo app? • Off-load the Tornado app by letting the Flask app asynchronously publish the message in order to increase the message turnover rate in Tornado app. • Use pika.adapters.TornadoConnection for proper integration with Tornado Framework.(http:// pika.readthedocs.org/en/latest/examples/ tornado_consumer.html) • and many more. 41
  • 42. And, no more demo Otherwise, this will be WebSocket 102. 42
  • 43. Alternatives? flask-sockets gevent-websocket flask-socketio autobahn your imagination 43
  • 45. All photographs are taken and copyrighted by Juti Noppornpitak.