Websockets in Flask on Heroku
(With inspiration from https://github.com/jamesward/hello-python-flask)
Note: this example is taken directly from Miguel Grinberg's example. It required slight adaptation (see notes). The goal was just to get it going, and then see if it could be launched on Heroku.
Some of the libraries involved here are only for Python 2.7, so you must first install that version of Python.
Although the most recent versions of Python do install pip, for some reason it's an old version. To get the latest version of pip:
python -m pip install -U pip
Next, make sure you have virtualenv installed:
If this gives you an error message, run:
pip install virtualenv
Quick Start on Windows
(Tested on Windows 7 and Windows 8).
To experiment locally on Windows (before deploying on Heroku) I've gone through the various thrashings-around and tried to capture and simplify things.
Flask-SocketIO relies on gevent which in turn relies on greenlet, both of which are binaries. You can figure out how to install the right Microsoft Visual C++ Compiler and configure it so that you don't get the message about not finding vcvarsall.bat file, but I couldn't figure that out and I eventually discovered that I could install directly from the wheel files. I've included the necessary wheel files in this distribution, and you'll see that in setup2.bat the pip commands that directly install the wheel files precede the pip install using requirements.txt. This seems to work (but note that my particular wheel files might be out of date when you read this).
Now, for your local Windows testing you should be able to run:
And then be ready to start the app:
If that succeeds, you should be able to open a local page and see the results.
The go.bat and bye.bat allow you to quickly enter and leave the virtualenv.
The Heroku deploy ignores the .bat files and just uses the requirements.txt which does the normal builds (using Heroku's C++ compiler).
Run Locally on Mac/Linux
virtualenv venv --distribute
Get the dependencies:
pip install -r requirements.txt
Start Flask Server
Test out the app by opening a local page to see the results.
Run on Heroku
Create the app
heroku create -s cedar
Deploy the app
git push heroku master
Open the app in your browser
Log onto the Heroku dashboard, then follow the instructions to connect to your github repository and launch your app. This can include auto-deployment every time you update your Github repository!
- The app running on Heroku.
- Flask main site.
- Flask documentation.
- The Flask-SocketIO Github page.
- The Flask-SocketIO Docs.
- Miguel Grinberg's tutorial.
- Two things needed changing to make the example work on Heroku:
- When you change main.py, Flask's automatic refresh doesn't work. You have to kill it and restart it to see the results. The refresh only seems to work on templates.
- Here's information from Heroku on using websockets with python.
- Here are lots of prebuilt wheel binaries.
- Here is a special version of Microsoft Visual C++ for Python 2.7. After installing it I still got the "cannot find vcvarsall.bat" error so I gave up and went with wheels instead.
- An alternative Flask websocket library.
- If you run pip freeze once everything is running (inside a virtualenv so you don't get anything that isn't necessary for your project), you can redirect the results right into your requirements.txt. However note that in this project most of those lines are redundant.
- If you deploy to Heroku and it doesn't work you should view the logs.
- Virtualenvwrapper is designed to make virtualenv easier to use. However, it's designed for *nix-based systems.