| 1 |
Deploy to Heroku |
| 2 |
================ |
| 3 |
|
| 4 |
This guide describes how to deploy a websockets server to Heroku_. The same |
| 5 |
principles should apply to other Platform as a Service providers. |
| 6 |
|
| 7 |
.. _Heroku: https://www.heroku.com/ |
| 8 |
|
| 9 |
.. admonition:: Heroku no longer offers a free tier. |
| 10 |
:class: attention |
| 11 |
|
| 12 |
When this tutorial was written, in September 2021, Heroku offered a free |
| 13 |
tier where a websockets app could run at no cost. In November 2022, Heroku |
| 14 |
removed the free tier, making it impossible to maintain this document. As a |
| 15 |
consequence, it isn't updated anymore and may be removed in the future. |
| 16 |
|
| 17 |
We're going to deploy a very simple app. The process would be identical for a |
| 18 |
more realistic app. |
| 19 |
|
| 20 |
Create repository |
| 21 |
----------------- |
| 22 |
|
| 23 |
Deploying to Heroku requires a git repository. Let's initialize one: |
| 24 |
|
| 25 |
.. code-block:: console |
| 26 |
|
| 27 |
$ mkdir websockets-echo |
| 28 |
$ cd websockets-echo |
| 29 |
$ git init -b main |
| 30 |
Initialized empty Git repository in websockets-echo/.git/ |
| 31 |
$ git commit --allow-empty -m "Initial commit." |
| 32 |
[main (root-commit) 1e7947d] Initial commit. |
| 33 |
|
| 34 |
Create application |
| 35 |
------------------ |
| 36 |
|
| 37 |
Here's the implementation of the app, an echo server. Save it in a file called |
| 38 |
``app.py``: |
| 39 |
|
| 40 |
.. literalinclude:: ../../example/deployment/heroku/app.py |
| 41 |
:language: python |
| 42 |
|
| 43 |
Heroku expects the server to `listen on a specific port`_, which is provided |
| 44 |
in the ``$PORT`` environment variable. The app reads it and passes it to |
| 45 |
:func:`~websockets.asyncio.server.serve`. |
| 46 |
|
| 47 |
.. _listen on a specific port: https://devcenter.heroku.com/articles/preparing-a-codebase-for-heroku-deployment#4-listen-on-the-correct-port |
| 48 |
|
| 49 |
Heroku sends a ``SIGTERM`` signal to all processes when `shutting down a |
| 50 |
dyno`_. When the app receives this signal, it closes connections and exits |
| 51 |
cleanly. |
| 52 |
|
| 53 |
.. _shutting down a dyno: https://devcenter.heroku.com/articles/dynos#shutdown |
| 54 |
|
| 55 |
Create a ``requirements.txt`` file containing this line to declare a dependency |
| 56 |
on websockets: |
| 57 |
|
| 58 |
.. literalinclude:: ../../example/deployment/heroku/requirements.txt |
| 59 |
:language: text |
| 60 |
|
| 61 |
Create a ``Procfile`` to tell Heroku how to run the app. |
| 62 |
|
| 63 |
.. literalinclude:: ../../example/deployment/heroku/Procfile |
| 64 |
|
| 65 |
Confirm that you created the correct files and commit them to git: |
| 66 |
|
| 67 |
.. code-block:: console |
| 68 |
|
| 69 |
$ ls |
| 70 |
Procfile app.py requirements.txt |
| 71 |
$ git add . |
| 72 |
$ git commit -m "Initial implementation." |
| 73 |
[main 8418c62] Initial implementation. |
| 74 |
3 files changed, 32 insertions(+) |
| 75 |
create mode 100644 Procfile |
| 76 |
create mode 100644 app.py |
| 77 |
create mode 100644 requirements.txt |
| 78 |
|
| 79 |
The app is ready. Let's deploy it! |
| 80 |
|
| 81 |
Deploy application |
| 82 |
------------------ |
| 83 |
|
| 84 |
Follow the instructions_ to install the Heroku CLI, if you haven't done that |
| 85 |
yet. |
| 86 |
|
| 87 |
.. _instructions: https://devcenter.heroku.com/articles/getting-started-with-python#set-up |
| 88 |
|
| 89 |
Sign up or log in to Heroku. |
| 90 |
|
| 91 |
Create a Heroku app — you'll have to pick a different name because I'm already |
| 92 |
using ``websockets-echo``: |
| 93 |
|
| 94 |
.. code-block:: console |
| 95 |
|
| 96 |
$ heroku create websockets-echo |
| 97 |
Creating ⬢ websockets-echo... done |
| 98 |
https://websockets-echo.herokuapp.com/ | https://git.heroku.com/websockets-echo.git |
| 99 |
|
| 100 |
.. code-block:: console |
| 101 |
|
| 102 |
$ git push heroku |
| 103 |
|
| 104 |
... lots of output... |
| 105 |
|
| 106 |
remote: -----> Launching... |
| 107 |
remote: Released v1 |
| 108 |
remote: https://websockets-echo.herokuapp.com/ deployed to Heroku |
| 109 |
remote: |
| 110 |
remote: Verifying deploy... done. |
| 111 |
To https://git.heroku.com/websockets-echo.git |
| 112 |
* [new branch] main -> main |
| 113 |
|
| 114 |
Validate deployment |
| 115 |
------------------- |
| 116 |
|
| 117 |
Let's confirm that your application is running as expected. |
| 118 |
|
| 119 |
Since it's a WebSocket server, you need a WebSocket client, such as the |
| 120 |
interactive client that comes with websockets. |
| 121 |
|
| 122 |
If you're currently building a websockets server, perhaps you're already in a |
| 123 |
virtualenv where websockets is installed. If not, you can install it in a new |
| 124 |
virtualenv as follows: |
| 125 |
|
| 126 |
.. code-block:: console |
| 127 |
|
| 128 |
$ python -m venv websockets-client |
| 129 |
$ . websockets-client/bin/activate |
| 130 |
$ pip install websockets |
| 131 |
|
| 132 |
Connect the interactive client — you must replace ``websockets-echo`` with the |
| 133 |
name of your Heroku app in this command: |
| 134 |
|
| 135 |
.. code-block:: console |
| 136 |
|
| 137 |
$ websockets wss://websockets-echo.herokuapp.com/ |
| 138 |
Connected to wss://websockets-echo.herokuapp.com/. |
| 139 |
> |
| 140 |
|
| 141 |
Great! Your app is running! |
| 142 |
|
| 143 |
Once you're connected, you can send any message and the server will echo it, |
| 144 |
or press Ctrl-D to terminate the connection: |
| 145 |
|
| 146 |
.. code-block:: console |
| 147 |
|
| 148 |
> Hello! |
| 149 |
< Hello! |
| 150 |
Connection closed: 1000 (OK). |
| 151 |
|
| 152 |
You can also confirm that your application shuts down gracefully. |
| 153 |
|
| 154 |
Connect an interactive client again — remember to replace ``websockets-echo`` |
| 155 |
with your app: |
| 156 |
|
| 157 |
.. code-block:: console |
| 158 |
|
| 159 |
$ websockets wss://websockets-echo.herokuapp.com/ |
| 160 |
Connected to wss://websockets-echo.herokuapp.com/. |
| 161 |
> |
| 162 |
|
| 163 |
In another shell, restart the app — again, replace ``websockets-echo`` with your |
| 164 |
app: |
| 165 |
|
| 166 |
.. code-block:: console |
| 167 |
|
| 168 |
$ heroku dyno:restart -a websockets-echo |
| 169 |
Restarting dynos on ⬢ websockets-echo... done |
| 170 |
|
| 171 |
Go back to the first shell. The connection is closed with code 1001 (going |
| 172 |
away). |
| 173 |
|
| 174 |
.. code-block:: console |
| 175 |
|
| 176 |
$ websockets wss://websockets-echo.herokuapp.com/ |
| 177 |
Connected to wss://websockets-echo.herokuapp.com/. |
| 178 |
Connection closed: 1001 (going away). |
| 179 |
|
| 180 |
If graceful shutdown wasn't working, the server wouldn't perform a closing |
| 181 |
handshake and the connection would be closed with code 1006 (abnormal closure). |