| 1 |
Part 3 - Deploy to the web |
| 2 |
========================== |
| 3 |
|
| 4 |
.. currentmodule:: websockets |
| 5 |
|
| 6 |
.. admonition:: This is the third part of the tutorial. |
| 7 |
|
| 8 |
* In the :doc:`first part <tutorial1>`, you created a server and |
| 9 |
connected one browser; you could play if you shared the same browser. |
| 10 |
* In this :doc:`second part <tutorial2>`, you connected a second browser; |
| 11 |
you could play from different browsers on a local network. |
| 12 |
* In this :doc:`third part <tutorial3>`, you will deploy the game to the |
| 13 |
web; you can play from any browser connected to the Internet. |
| 14 |
|
| 15 |
In the first and second parts of the tutorial, for local development, you ran |
| 16 |
an HTTP server on ``http://localhost:8000/`` with: |
| 17 |
|
| 18 |
.. code-block:: console |
| 19 |
|
| 20 |
$ python -m http.server |
| 21 |
|
| 22 |
and a WebSocket server on ``ws://localhost:8001/`` with: |
| 23 |
|
| 24 |
.. code-block:: console |
| 25 |
|
| 26 |
$ python app.py |
| 27 |
|
| 28 |
Now you want to deploy these servers on the Internet. There's a vast range of |
| 29 |
hosting providers to choose from. For the sake of simplicity, we'll rely on: |
| 30 |
|
| 31 |
* GitHub Pages for the HTTP server; |
| 32 |
* Heroku for the WebSocket server. |
| 33 |
|
| 34 |
Commit project to git |
| 35 |
--------------------- |
| 36 |
|
| 37 |
Perhaps you committed your work to git while you were progressing through the |
| 38 |
tutorial. If you didn't, now is a good time, because GitHub and Heroku offer |
| 39 |
git-based deployment workflows. |
| 40 |
|
| 41 |
Initialize a git repository: |
| 42 |
|
| 43 |
.. code-block:: console |
| 44 |
|
| 45 |
$ git init -b main |
| 46 |
Initialized empty Git repository in websockets-tutorial/.git/ |
| 47 |
$ git commit --allow-empty -m "Initial commit." |
| 48 |
[main (root-commit) ...] Initial commit. |
| 49 |
|
| 50 |
Add all files and commit: |
| 51 |
|
| 52 |
.. code-block:: console |
| 53 |
|
| 54 |
$ git add . |
| 55 |
$ git commit -m "Initial implementation of Connect Four game." |
| 56 |
[main ...] Initial implementation of Connect Four game. |
| 57 |
6 files changed, 500 insertions(+) |
| 58 |
create mode 100644 app.py |
| 59 |
create mode 100644 connect4.css |
| 60 |
create mode 100644 connect4.js |
| 61 |
create mode 100644 connect4.py |
| 62 |
create mode 100644 index.html |
| 63 |
create mode 100644 main.js |
| 64 |
|
| 65 |
Prepare the WebSocket server |
| 66 |
---------------------------- |
| 67 |
|
| 68 |
Before you deploy the server, you must adapt it to meet requirements of |
| 69 |
Heroku's runtime. This involves two small changes: |
| 70 |
|
| 71 |
1. Heroku expects the server to `listen on a specific port`_, provided in the |
| 72 |
``$PORT`` environment variable. |
| 73 |
|
| 74 |
2. Heroku sends a ``SIGTERM`` signal when `shutting down a dyno`_, which |
| 75 |
should trigger a clean exit. |
| 76 |
|
| 77 |
.. _listen on a specific port: https://devcenter.heroku.com/articles/preparing-a-codebase-for-heroku-deployment#4-listen-on-the-correct-port |
| 78 |
|
| 79 |
.. _shutting down a dyno: https://devcenter.heroku.com/articles/dynos#shutdown |
| 80 |
|
| 81 |
Adapt the ``main()`` coroutine accordingly: |
| 82 |
|
| 83 |
.. code-block:: python |
| 84 |
|
| 85 |
import os |
| 86 |
import signal |
| 87 |
|
| 88 |
.. literalinclude:: ../../example/tutorial/step3/app.py |
| 89 |
:pyobject: main |
| 90 |
|
| 91 |
To catch the ``SIGTERM`` signal, ``main()`` creates a :class:`~asyncio.Future` |
| 92 |
called ``stop`` and registers a signal handler that sets the result of this |
| 93 |
future. The value of the future doesn't matter; it's only for waiting for |
| 94 |
``SIGTERM``. |
| 95 |
|
| 96 |
Then, by using :func:`~server.serve` as a context manager and exiting the |
| 97 |
context when ``stop`` has a result, ``main()`` ensures that the server closes |
| 98 |
connections cleanly and exits on ``SIGTERM``. |
| 99 |
|
| 100 |
The app is now fully compatible with Heroku. |
| 101 |
|
| 102 |
Deploy the WebSocket server |
| 103 |
--------------------------- |
| 104 |
|
| 105 |
Create a ``requirements.txt`` file with this content to install ``websockets`` |
| 106 |
when building the image: |
| 107 |
|
| 108 |
.. literalinclude:: ../../example/tutorial/step3/requirements.txt |
| 109 |
:language: text |
| 110 |
|
| 111 |
.. admonition:: Heroku treats ``requirements.txt`` as a signal to `detect a Python app`_. |
| 112 |
:class: tip |
| 113 |
|
| 114 |
That's why you don't need to declare that you need a Python runtime. |
| 115 |
|
| 116 |
.. _detect a Python app: https://devcenter.heroku.com/articles/python-support#recognizing-a-python-app |
| 117 |
|
| 118 |
Create a ``Procfile`` file with this content to configure the command for |
| 119 |
running the server: |
| 120 |
|
| 121 |
.. literalinclude:: ../../example/tutorial/step3/Procfile |
| 122 |
:language: text |
| 123 |
|
| 124 |
Commit your changes: |
| 125 |
|
| 126 |
.. code-block:: console |
| 127 |
|
| 128 |
$ git add . |
| 129 |
$ git commit -m "Deploy to Heroku." |
| 130 |
[main ...] Deploy to Heroku. |
| 131 |
3 files changed, 12 insertions(+), 2 deletions(-) |
| 132 |
create mode 100644 Procfile |
| 133 |
create mode 100644 requirements.txt |
| 134 |
|
| 135 |
Follow the `set-up instructions`_ to install the Heroku CLI and to log in, if |
| 136 |
you haven't done that yet. |
| 137 |
|
| 138 |
.. _set-up instructions: https://devcenter.heroku.com/articles/getting-started-with-python#set-up |
| 139 |
|
| 140 |
Create a Heroku app. You must choose a unique name and replace |
| 141 |
``websockets-tutorial`` by this name in the following command: |
| 142 |
|
| 143 |
.. code-block:: console |
| 144 |
|
| 145 |
$ heroku create websockets-tutorial |
| 146 |
Creating ⬢ websockets-tutorial... done |
| 147 |
https://websockets-tutorial.herokuapp.com/ | https://git.heroku.com/websockets-tutorial.git |
| 148 |
|
| 149 |
If you reuse a name that someone else already uses, you will receive this |
| 150 |
error; if this happens, try another name: |
| 151 |
|
| 152 |
.. code-block:: console |
| 153 |
|
| 154 |
$ heroku create websockets-tutorial |
| 155 |
Creating ⬢ websockets-tutorial... ! |
| 156 |
▸ Name websockets-tutorial is already taken |
| 157 |
|
| 158 |
Deploy by pushing the code to Heroku: |
| 159 |
|
| 160 |
.. code-block:: console |
| 161 |
|
| 162 |
$ git push heroku |
| 163 |
|
| 164 |
... lots of output... |
| 165 |
|
| 166 |
remote: Released v1 |
| 167 |
remote: https://websockets-tutorial.herokuapp.com/ deployed to Heroku |
| 168 |
remote: |
| 169 |
remote: Verifying deploy... done. |
| 170 |
To https://git.heroku.com/websockets-tutorial.git |
| 171 |
* [new branch] main -> main |
| 172 |
|
| 173 |
You can test the WebSocket server with the interactive client exactly like you |
| 174 |
did in the first part of the tutorial. Replace ``websockets-tutorial`` by the |
| 175 |
name of your app in the following command: |
| 176 |
|
| 177 |
.. code-block:: console |
| 178 |
|
| 179 |
$ python -m websockets wss://websockets-tutorial.herokuapp.com/ |
| 180 |
Connected to wss://websockets-tutorial.herokuapp.com/. |
| 181 |
> {"type": "init"} |
| 182 |
< {"type": "init", "join": "54ICxFae_Ip7TJE2", "watch": "634w44TblL5Dbd9a"} |
| 183 |
Connection closed: 1000 (OK). |
| 184 |
|
| 185 |
It works! |
| 186 |
|
| 187 |
Prepare the web application |
| 188 |
--------------------------- |
| 189 |
|
| 190 |
Before you deploy the web application, perhaps you're wondering how it will |
| 191 |
locate the WebSocket server? Indeed, at this point, its address is hard-coded |
| 192 |
in ``main.js``: |
| 193 |
|
| 194 |
.. code-block:: javascript |
| 195 |
|
| 196 |
const websocket = new WebSocket("ws://localhost:8001/"); |
| 197 |
|
| 198 |
You can take this strategy one step further by checking the address of the |
| 199 |
HTTP server and determining the address of the WebSocket server accordingly. |
| 200 |
|
| 201 |
Add this function to ``main.js``; replace ``python-websockets`` by your GitHub |
| 202 |
username and ``websockets-tutorial`` by the name of your app on Heroku: |
| 203 |
|
| 204 |
.. literalinclude:: ../../example/tutorial/step3/main.js |
| 205 |
:language: js |
| 206 |
:start-at: function getWebSocketServer |
| 207 |
:end-before: function initGame |
| 208 |
|
| 209 |
Then, update the initialization to connect to this address instead: |
| 210 |
|
| 211 |
.. code-block:: javascript |
| 212 |
|
| 213 |
const websocket = new WebSocket(getWebSocketServer()); |
| 214 |
|
| 215 |
Commit your changes: |
| 216 |
|
| 217 |
.. code-block:: console |
| 218 |
|
| 219 |
$ git add . |
| 220 |
$ git commit -m "Configure WebSocket server address." |
| 221 |
[main ...] Configure WebSocket server address. |
| 222 |
1 file changed, 11 insertions(+), 1 deletion(-) |
| 223 |
|
| 224 |
Deploy the web application |
| 225 |
-------------------------- |
| 226 |
|
| 227 |
Go to GitHub and create a new repository called ``websockets-tutorial``. |
| 228 |
|
| 229 |
Push your code to this repository. You must replace ``python-websockets`` by |
| 230 |
your GitHub username in the following command: |
| 231 |
|
| 232 |
.. code-block:: console |
| 233 |
|
| 234 |
$ git remote add origin [email protected]:python-websockets/websockets-tutorial.git |
| 235 |
$ git push -u origin main |
| 236 |
Enumerating objects: 11, done. |
| 237 |
Counting objects: 100% (11/11), done. |
| 238 |
Delta compression using up to 8 threads |
| 239 |
Compressing objects: 100% (10/10), done. |
| 240 |
Writing objects: 100% (11/11), 5.90 KiB | 2.95 MiB/s, done. |
| 241 |
Total 11 (delta 0), reused 0 (delta 0), pack-reused 0 |
| 242 |
To github.com:<username>/websockets-tutorial.git |
| 243 |
* [new branch] main -> main |
| 244 |
Branch 'main' set up to track remote branch 'main' from 'origin'. |
| 245 |
|
| 246 |
Go back to GitHub, open the Settings tab of the repository and select Pages in |
| 247 |
the menu. Select the main branch as source and click Save. GitHub tells you |
| 248 |
that your site is published. |
| 249 |
|
| 250 |
Follow the link and start a game! |
| 251 |
|
| 252 |
Summary |
| 253 |
------- |
| 254 |
|
| 255 |
In this third part of the tutorial, you learned how to deploy a WebSocket |
| 256 |
application with Heroku. |
| 257 |
|
| 258 |
You can start a Connect Four game, send the JOIN link to a friend, and play |
| 259 |
over the Internet! |
| 260 |
|
| 261 |
Congratulations for completing the tutorial. Enjoy building real-time web |
| 262 |
applications with websockets! |
| 263 |
|
| 264 |
Solution |
| 265 |
-------- |
| 266 |
|
| 267 |
.. literalinclude:: ../../example/tutorial/step3/app.py |
| 268 |
:caption: app.py |
| 269 |
:language: python |
| 270 |
:linenos: |
| 271 |
|
| 272 |
.. literalinclude:: ../../example/tutorial/step3/index.html |
| 273 |
:caption: index.html |
| 274 |
:language: html |
| 275 |
:linenos: |
| 276 |
|
| 277 |
.. literalinclude:: ../../example/tutorial/step3/main.js |
| 278 |
:caption: main.js |
| 279 |
:language: js |
| 280 |
:linenos: |
| 281 |
|
| 282 |
.. literalinclude:: ../../example/tutorial/step3/Procfile |
| 283 |
:caption: Procfile |
| 284 |
:language: text |
| 285 |
:linenos: |
| 286 |
|
| 287 |
.. literalinclude:: ../../example/tutorial/step3/requirements.txt |
| 288 |
:caption: requirements.txt |
| 289 |
:language: text |
| 290 |
:linenos: |