<divstyle="padding: 0 0 20px 0; margin: 0 0 20px 0; background-color: #22B24C;">
<divclass="container">
<h2style="color: #fff; margin: 20px 40px 0 0;"><ahref="https://www.gumroad.com/l/python-deployments" style="color: #fff">The Full Stack Python Guide to Deployments book</a> has been released!</h2>
</div>
</div><ahref="https://github.com/makaimc/fullstackpython.com"><imgstyle="position: absolute; top: 0; right: 0; border: 0;" src="/theme/img/fork.png" alt="Fork me on GitHub"></a>
<ahref="https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-12">WebRTC data channels</a>,
are important is that HTTP is not meant for keeping open a connection for
the server to frequently push data to a web browser. Previously, most web
applications would implement long polling via frequent
Asynchronous JavaScript and XML (AJAX) requests as shown in the below diagram. </p>
<p><imgsrc="theme/img/ajax-long-polling.png" width="100%" alt="Long polling via AJAX is incredibly inefficient for some applications." class="technical-diagram" /></p>
<p>Server push is more efficient and scalable than long polling because the
web browser does not have to constantly ask for updates through a stream
of AJAX requests.</p>
<p><imgsrc="theme/img/websockets-flow.png" width="100%" alt="WebSockets are more efficient than long polling for server sent updates." class="technical-diagram" /></p>
<p>While the above diagram shows a server pushing data to the client, WebSockets
is a full-duplex connection so the client can also push data to the server
as shown in the diagram below.</p>
<p><imgsrc="theme/img/websockets-flow-with-client-push.png" width="100%" alt="WebSockets also allow client push in addition to server pushed updates." class="technical-diagram" /></p>
<p>The WebSockets approach for server- and client-pushed updates works well for
certain categories of web applications such as chat room, which is why that's
often an example application for a WebSocket library.</p>
<h2>Implementing WebSockets</h2>
<p>Both the web browser and the server must implement the WebSockets protocol
to establish and maintain the connection. There are important implications for
servers since WebSockets connections are long lived, unlike typical HTTP
connections. </p>
<p>A multi-threaded or multi-process based server cannot scale appropriately for
WebSockets because it is designed to open a connection, handle a request as
quickly as possible and then close the connection. An asynchronous server such
as <ahref="http://www.tornadoweb.org/en/stable/">Tornado</a> or
<ahref="http://gunicorn.org/">Green Unicorn</a> monkey patched with
<ahref="http://www.gevent.org/">gevent</a> is necessary for any practical WebSockets
server-side implementation.</p>
<p>On the client side, it is not necessary to use a JavaScript library for
WebSockets. Web browsers that implement WebSockets will expose all necessary
for which web browsers and specific versions support WebSockets.</p>
</li>
<li>
<p>Mozilla's
<ahref="https://developer.mozilla.org/en-US/docs/WebSockets">Developer Resources for WebSockets</a>
is a good place to find documentation and tools for developing with
WebSockets.</p>
</li>
<li>
<p><ahref="http://websocketd.com/">websocketd</a> is a WebSockets server aiming to be
the "CGI of WebSockets". Worth a look.</p>
</li>
</ul>
<h2>Python-specific WebSockets resources</h2>
<ul>
<li>
<p><ahref="http://mrjoes.github.io/2013/06/21/python-realtime.html">Real-time in Python</a>
provides Python-specific context for how the server push updates were
implemented in the past and how Python's tools have evolved to perform
server side updates.</p>
</li>
<li>
<p><ahref="https://github.com/aaugustin/websockets">websockets</a> is a WebSockets
implementation for Python 3.3+ written with the
<ahref="https://docs.python.org/3.4/library/asyncio.html">asyncio</a> module (or with
<ahref="https://code.google.com/p/tulip/">Tulip</a> if you're working with
Python 3.3).</p>
</li>
<li>
<p>The <ahref="https://www.twilio.com/blog/2014/11/choose-your-own-adventure-presentations-with-reveal-js-python-and-websockets.html">Choose Your Own Adventure Presentations</a>
tutorial uses WebSockets via gevent on the server and socketio.js for
pushing vote count updates from the server to the client. </p>
</li>
<li>
<p><ahref="http://crossbar.io/docs/Adding-Real-Time-to-Django-Applications/">Adding Real Time to Django Applications</a>
shows how to use Django and Crossbar.io to implement a publish/subscribe
feature in the application.</p>
</li>
<li>
<p><ahref="http://bottlepy.org/docs/dev/async.html">Async with Bottle</a> shows how to
use greenlets to support WebSockets with the Bottle web framework.</p>
<ahref="http://www.deploypython.com/"><imgsrc="//static.fullstackpython.com/fsp-deployment-guide.png" alt="The Full Stack Python Guide to Deployments" width="100%"></a>
<pstyle="font-size: .8em; margin-top: 10px;">Searching for a complete, step-by-step deployment walkthrough? Learn more about <ahref="http://www.deploypython.com/">The Full Stack Python Guide to Deployments book</a>.
</p>
</div>
</div><divclass="panel panel-success">
<divclass="panel-heading">
<h3class="panel-head"><ahref="/table-of-contents.html" style="color: #fff;">Table of Contents</a></h3>
<ahref="/about-author.html" class="list-group-item smaller-item " style='font-family: "Helvetica Neue",sans-serif;'>About the Author</a>
</div>
</div>
<divclass="panel panel-success">
<divclass="panel-body">
<ahref="http://www.deploypython.com/"><imgsrc="//static.fullstackpython.com/fsp-deployment-guide.png" alt="The Full Stack Python Guide to Deployments" width="100%"></a>
<pstyle="font-size: .8em; margin-top: 10px;">Searching for a complete, step-by-step deployment walkthrough? Learn more about <ahref="http://www.deploypython.com/">The Full Stack Python Guide to Deployments book</a>.