WebSockets with Python at the Forge

Central Ohio has a fairly active Python community thanks to the Central Ohio Python Users Group.  I’ve been encouraged to check out the meetings and dojo’s by a couple members over the last year, so when I saw that the April 2013 meeting topic was WebSockets, it was time to attend.  It also just happened to be the first time that the group was meeting at the Columbus Forge, Pillar Technology’s new facility near downtown Columbus.

I’d been to the Forge a couple times since its opening this year.  My first visit was attending “A Day in the Life of Agile”, an agile training session that I enjoyed and highly recommend.  I’d also hit a lunch meeting of the Acceptance Test Driven Development (ATDD) group, so I knew the facility was beautiful and the food/beer was great!

Pillar has developed the Forge as a retreat to foster and develop creativity.  Bob Myers, the CEO of Pillar, has been kind enough each visit to give his mini tour and explain the need and implementation of the space.  It really is an awesome facility in the old Smith Brothers Hardware Building where Pillar has been kind enough to offer local user groups meeting space.  I encourage everyone to check out a local tech group meeting and have one of the excellent beers on tap.

Back to WebSockets.

BSD Sockets have been around since 1983 when they first appeared in BSD Unix.  HTTP utilizes sockets as an underlying communications method for its request/response model.  AJAX follows this same model.  Unfortunately, these have been unidirectional sockets that follow the HTTP request/response model.  Meaning the browser can initiate communications with the server, but the server has not been able to initiate communications with the browser.

A few methods have been developed over the years to mitigate this communications deficiency.  These have included polling where the client requests updates every N seconds, long-polling where specialized client and server components (Continuations and CometD) hold open the unidirectional socket, and HTTP streaming.  These methods tend to place high load on the server and require a deeper expertise of developers.  This has all changed with HTML 5.  The HTML 5 specification includes full support via WebSockets, bringing traditional bidirectional BSD style sockets to the browser.

Python libraries exist for developing traditional BSD sockets as well as WebSockets.  WebSocket supporting libraries include SocketIO and Ws4Py, as well as the traditional full asynchronous libraries Twisted and Gevent.  Utilizing a WebSocket specific library encapsulates the underlying BSD socket library and affords the programmer a more intuitive methodology.  Brian Costlow discussed the various python libraries available and was kind enough to demonstrate utilizing WebSockets for both a thick Python based client and a thin browser based client.

The thick client pushed a series of numbers from the client to the server.  The server then echo’d these back to the client.  A simple reproducible example that demonstrated the potential of WebSockets.  His browser based example demonstrated a simple chat application with a pair of browser instances.  Of particular interests was his discussion of fallback mechanisms that identify legacy browser clients and implement polling techniques to mitigate the lack of support in the client.

It was a great presentation that left me wanting more.  I’ll be looking into Python’s support of traditional BSD sockets and stopping out at a Dojo to implement a couple simple examples of WebSockets.

A special thanks to Ben Rogers of Pillar for hanging around and engaging in cleanup. About a dozen of us adjourned to Barley’s for more food and beer. A great outing! I’ll be attending more Central Ohio Python meetings in the future.

As a final note, look into the PyOhio conference. It’s the second largest python conference in North America and it’s happening here in Columbus July 27-28, 2013.  Oh Yea, It’s FREE!

Leave a Reply