KEMBAR78
Implement server push in flask framework | PPTX
Implement Server Push 
in 
Chi-Chia Huang (黃啟嘉) 
Oct 24 2014
Flask is a microframework for Python based on Werkzeug, Jinja 2
Notification from server 
3
client Server 
4
client Server 
5
Server Push Approaches 
• Polling 
• Long Polling 
• Server Sent Events 
• Websocket 
http://mrjoes.github.io/2013/06/21/python-realtime.html 
6
Time Server/Client 
polling, long polling, server sent events, websocket 
7
Polling 
Noise to signal ration is usually very high… 
8 
Client
Long Polling 
9 
Client
Long Polling 
10 
Server 
use POST to prevent the browser 
from cache the request
Server Sent Events 
Client 
11
Server Sent Events 
12 
Server 
The event stream is a simple stream of text data, 
which must be encoded using UTF-8. Each 
message is separated by a pair of newline 
characters. 
https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events#Event_stream_format
Event Stream Example 
https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events#Event_stream_format 
13
14
Websocket[RFC6445] 
Client 
15
Websocket[RFC6445] 
16 
Server
Which one is better? 
17 
• Server-Sent Events 
• Are sent over traditional HTTP 
• Automatic reconnection 
• Ability to send arbitrary events 
• WebSockets 
• Bi-directional, full-duplex communication
Chatroom Example 
• Polling approach: 
• http://chichia.aws.prd.demodesu.com:5002/type/polling 
• Long Polling approach: 
• http://chichia.aws.prd.demodesu.com:5002/type/lpolling 
• Server Sent Event approach: 
• http://chichia.aws.prd.demodesu.com:5002/type/sse 
• Websocket approach: 
• http://chichia.aws.prd.demodesu.com:5002/type/ws 
18
Intern Training Project 
• http://chichia.aws.prd.demodesu.com:8080/ 
19
References 
• Long Polling 
• Jonathan Eunice lpoll 
• AlanYang gevent-flask-comet 
• Websocket 
• Jonathan Eunice flask-ws-example 
• Server Sent Event 
• Oskar Blom Realtime Server Using The SSE Protocol 
• Eric Bidelman Stream Updates with Server-Sent Events 
• MDN Using Server Sent Event
& 
21

Implement server push in flask framework

  • 1.
    Implement Server Push in Chi-Chia Huang (黃啟嘉) Oct 24 2014
  • 2.
    Flask is amicroframework for Python based on Werkzeug, Jinja 2
  • 3.
  • 4.
  • 5.
  • 6.
    Server Push Approaches • Polling • Long Polling • Server Sent Events • Websocket http://mrjoes.github.io/2013/06/21/python-realtime.html 6
  • 7.
    Time Server/Client polling,long polling, server sent events, websocket 7
  • 8.
    Polling Noise tosignal ration is usually very high… 8 Client
  • 9.
  • 10.
    Long Polling 10 Server use POST to prevent the browser from cache the request
  • 11.
  • 12.
    Server Sent Events 12 Server The event stream is a simple stream of text data, which must be encoded using UTF-8. Each message is separated by a pair of newline characters. https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events#Event_stream_format
  • 13.
    Event Stream Example https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events#Event_stream_format 13
  • 14.
  • 15.
  • 16.
  • 17.
    Which one isbetter? 17 • Server-Sent Events • Are sent over traditional HTTP • Automatic reconnection • Ability to send arbitrary events • WebSockets • Bi-directional, full-duplex communication
  • 18.
    Chatroom Example •Polling approach: • http://chichia.aws.prd.demodesu.com:5002/type/polling • Long Polling approach: • http://chichia.aws.prd.demodesu.com:5002/type/lpolling • Server Sent Event approach: • http://chichia.aws.prd.demodesu.com:5002/type/sse • Websocket approach: • http://chichia.aws.prd.demodesu.com:5002/type/ws 18
  • 19.
    Intern Training Project • http://chichia.aws.prd.demodesu.com:8080/ 19
  • 20.
    References • LongPolling • Jonathan Eunice lpoll • AlanYang gevent-flask-comet • Websocket • Jonathan Eunice flask-ws-example • Server Sent Event • Oskar Blom Realtime Server Using The SSE Protocol • Eric Bidelman Stream Updates with Server-Sent Events • MDN Using Server Sent Event
  • 21.