SlideShare une entreprise Scribd logo
1  sur  45
Télécharger pour lire hors ligne
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.

Contenu connexe

Tendances

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
 

Tendances (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
 

Similaire à 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
 

Similaire à 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
 

Dernier

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 

Dernier (20)

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 

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.