Wednesday 9 October 2013

Heroku's Python Web Sockets Chat App Locally on Windows with Vagrant

Heroku have recently announced support for Web Sockets. They have provided a selection of code examples in various languages to illustrate the two-way communication enabled by web sockets. The focus of this article is the Python-Websockets-Chat application.



Provided is a walk-through for producing an app that will work on Heroku, taking advantage of the redis add-on that will be part of your infrastructure when your environment is created at set up. I wanted to get a better understanding of Python, Flask and the web sockets plugin, Gevent, so I needed to play with the application locally. This required some help from Vagrant. I've been over the benefits of Vagrant before in a similar article so no need to repeat myself.

The pre-requisites to having a running instance of the Python-Websockets-Chat application are numerous and complicated. Starting with a fresh Ubuntu Precise32 Vagrant box will take at least 15 minutes of install time to correctly configure the environment. Thankfully, the provisioning capabilities of Vagrant mean that we can kick off the 'Vagrant up' command in the project directory and just wait for the bootstrap shell script to do its thing.

The shell script is shown below. As you can see there are not only dependencies on python development tools and Pip - the Python plugin manager, but Ruby also. This is because the application is designed to run on Heroku which uses a Procfile to organise the processes that need deploying. Procfiles are best executed by a Ruby gem called Foreman, hence the dependency. Other than that there is the requirement to install and kick off Redis, followed by the Foreman start command to trigger the app into action. It should be available on your host machine at http://localhost:5000/.

Please see my forked repo of the chat app for all the code in one place.

Anyone with some experience in Linux will recognise a lot of this as incredibly basic instructions, understandably. This post is for the benefit of the Windows folk who have next to no knowledge of configuring Linux environments and just want to see results. That's part of Vagrant's goal, to simplify and centralize environment configuration, so I hope that this goes some way to helping that cause.

P.s. Windows users be sure that the line endings of the bootstrap.sh are in the linux format (LF) and not the Windows format (CRLF). Cloning the repo may have adjusted your line endings so if you run into a problem when the bootstrap.sh tries to be executed then please see this issue on github.

No comments:

Post a Comment