| 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 |
* Koyeb_ for the WebSocket server. |
| 33 |
|
| 34 |
.. _GitHub Pages: https://pages.github.com/ |
| 35 |
.. _Koyeb: https://www.koyeb.com/ |
| 36 |
|
| 37 |
Koyeb is a modern Platform as a Service provider whose free tier allows you to |
| 38 |
run a web application, including a WebSocket server. |
| 39 |
|
| 40 |
Commit project to git |
| 41 |
--------------------- |
| 42 |
|
| 43 |
Perhaps you committed your work to git while you were progressing through the |
| 44 |
tutorial. If you didn't, now is a good time, because GitHub and Koyeb offer |
| 45 |
git-based deployment workflows. |
| 46 |
|
| 47 |
Initialize a git repository: |
| 48 |
|
| 49 |
.. code-block:: console |
| 50 |
|
| 51 |
$ git init -b main |
| 52 |
Initialized empty Git repository in websockets-tutorial/.git/ |
| 53 |
$ git commit --allow-empty -m "Initial commit." |
| 54 |
[main (root-commit) 8195c1d] Initial commit. |
| 55 |
|
| 56 |
Add all files and commit: |
| 57 |
|
| 58 |
.. code-block:: console |
| 59 |
|
| 60 |
$ git add . |
| 61 |
$ git commit -m "Initial implementation of Connect Four game." |
| 62 |
[main 7f0b2c4] Initial implementation of Connect Four game. |
| 63 |
6 files changed, 500 insertions(+) |
| 64 |
create mode 100644 app.py |
| 65 |
create mode 100644 connect4.css |
| 66 |
create mode 100644 connect4.js |
| 67 |
create mode 100644 connect4.py |
| 68 |
create mode 100644 index.html |
| 69 |
create mode 100644 main.js |
| 70 |
|
| 71 |
Sign up or log in to GitHub. |
| 72 |
|
| 73 |
Create a new repository. Set the repository name to ``websockets-tutorial``, |
| 74 |
the visibility to Public, and click **Create repository**. |
| 75 |
|
| 76 |
Push your code to this repository. You must replace ``python-websockets`` by |
| 77 |
your GitHub username in the following command: |
| 78 |
|
| 79 |
.. code-block:: console |
| 80 |
|
| 81 |
$ git remote add origin [email protected]:python-websockets/websockets-tutorial.git |
| 82 |
$ git branch -M main |
| 83 |
$ git push -u origin main |
| 84 |
... |
| 85 |
To github.com:python-websockets/websockets-tutorial.git |
| 86 |
* [new branch] main -> main |
| 87 |
Branch 'main' set up to track remote branch 'main' from 'origin'. |
| 88 |
|
| 89 |
Adapt the WebSocket server |
| 90 |
-------------------------- |
| 91 |
|
| 92 |
Before you deploy the server, you must adapt it for Koyeb's environment. This |
| 93 |
involves three small changes: |
| 94 |
|
| 95 |
1. Koyeb provides the port on which the server should listen in the ``$PORT`` |
| 96 |
environment variable. |
| 97 |
|
| 98 |
2. Koyeb requires a health check to verify that the server is running. We'll add |
| 99 |
a HTTP health check. |
| 100 |
|
| 101 |
3. Koyeb sends a ``SIGTERM`` signal when terminating the server. We'll catch it |
| 102 |
and trigger a clean exit. |
| 103 |
|
| 104 |
Adapt the ``main()`` coroutine accordingly: |
| 105 |
|
| 106 |
.. code-block:: python |
| 107 |
|
| 108 |
import http |
| 109 |
import os |
| 110 |
import signal |
| 111 |
|
| 112 |
.. literalinclude:: ../../example/tutorial/step3/app.py |
| 113 |
:pyobject: health_check |
| 114 |
|
| 115 |
.. literalinclude:: ../../example/tutorial/step3/app.py |
| 116 |
:pyobject: main |
| 117 |
|
| 118 |
The ``process_request`` parameter of :func:`~asyncio.server.serve` is a callback |
| 119 |
that runs for each request. When it returns an HTTP response, websockets sends |
| 120 |
that response instead of opening a WebSocket connection. Here, requests to |
| 121 |
``/healthz`` return an HTTP 200 status code. |
| 122 |
|
| 123 |
``main()`` registers a signal handler that closes the server when receiving the |
| 124 |
``SIGTERM`` signal. Then, it waits for the server to be closed. Additionally, |
| 125 |
using :func:`~asyncio.server.serve` as a context manager ensures that the server |
| 126 |
will always be closed cleanly, even if the program crashes. |
| 127 |
|
| 128 |
Deploy the WebSocket server |
| 129 |
--------------------------- |
| 130 |
|
| 131 |
Create a ``requirements.txt`` file with this content to install ``websockets`` |
| 132 |
when building the image: |
| 133 |
|
| 134 |
.. literalinclude:: ../../example/tutorial/step3/requirements.txt |
| 135 |
:language: text |
| 136 |
|
| 137 |
.. admonition:: Koyeb treats ``requirements.txt`` as a signal to `detect a Python app`__. |
| 138 |
:class: tip |
| 139 |
|
| 140 |
That's why you don't need to declare that you need a Python runtime. |
| 141 |
|
| 142 |
__ https://www.koyeb.com/docs/build-and-deploy/build-from-git/python#detection |
| 143 |
|
| 144 |
Create a ``Procfile`` file with this content to configure the command for |
| 145 |
running the server: |
| 146 |
|
| 147 |
.. literalinclude:: ../../example/tutorial/step3/Procfile |
| 148 |
:language: text |
| 149 |
|
| 150 |
Commit and push your changes: |
| 151 |
|
| 152 |
.. code-block:: console |
| 153 |
|
| 154 |
$ git add . |
| 155 |
$ git commit -m "Deploy to Koyeb." |
| 156 |
[main 4a4b6e9] Deploy to Koyeb. |
| 157 |
3 files changed, 15 insertions(+), 2 deletions(-) |
| 158 |
create mode 100644 Procfile |
| 159 |
create mode 100644 requirements.txt |
| 160 |
$ git push |
| 161 |
... |
| 162 |
To github.com:python-websockets/websockets-tutorial.git |
| 163 |
+ 6bd6032...4a4b6e9 main -> main |
| 164 |
|
| 165 |
Sign up or log in to Koyeb. |
| 166 |
|
| 167 |
In the Koyeb control panel, create a web service with GitHub as the deployment |
| 168 |
method. `Install and authorize Koyeb's GitHub app`__ if you haven't done that yet. |
| 169 |
|
| 170 |
__ https://www.koyeb.com/docs/build-and-deploy/deploy-with-git#connect-your-github-account-to-koyeb |
| 171 |
|
| 172 |
Follow the steps to create a new service: |
| 173 |
|
| 174 |
1. Select the ``websockets-tutorial`` repository in the list of your repositories. |
| 175 |
2. Confirm that the **Free** instance type is selected. Click **Next**. |
| 176 |
3. Configure health checks: change the protocol from TCP to HTTP and set the |
| 177 |
path to ``/healthz``. Review other settings; defaults should be correct. |
| 178 |
Click **Deploy**. |
| 179 |
|
| 180 |
Koyeb builds the app, deploys it, verifies that the health checks passes, and |
| 181 |
makes the deployment active. |
| 182 |
|
| 183 |
You can test the WebSocket server with the interactive client exactly like you |
| 184 |
did in the first part of the tutorial. The Koyeb control panel provides the URL |
| 185 |
of your app in the format: ``https://<app>-<user>-<id>.koyeb.app/``. Replace |
| 186 |
``https`` with ``wss`` in the URL and connect the interactive client: |
| 187 |
|
| 188 |
.. code-block:: console |
| 189 |
|
| 190 |
$ websockets wss://<app>-<user>-<id>.koyeb.app/ |
| 191 |
Connected to wss://<app>-<user>-<id>.koyeb.app/. |
| 192 |
> {"type": "init"} |
| 193 |
< {"type": "init", "join": "54ICxFae_Ip7TJE2", "watch": "634w44TblL5Dbd9a"} |
| 194 |
|
| 195 |
Press Ctrl-D to terminate the connection. |
| 196 |
|
| 197 |
It works! |
| 198 |
|
| 199 |
Prepare the web application |
| 200 |
--------------------------- |
| 201 |
|
| 202 |
Before you deploy the web application, perhaps you're wondering how it will |
| 203 |
locate the WebSocket server? Indeed, at this point, its address is hard-coded |
| 204 |
in ``main.js``: |
| 205 |
|
| 206 |
.. code-block:: javascript |
| 207 |
|
| 208 |
const websocket = new WebSocket("ws://localhost:8001/"); |
| 209 |
|
| 210 |
You can take this strategy one step further by checking the address of the |
| 211 |
HTTP server and determining the address of the WebSocket server accordingly. |
| 212 |
|
| 213 |
Add this function to ``main.js``; replace ``python-websockets`` by your GitHub |
| 214 |
username and ``websockets-tutorial`` by the name of your app on Koyeb: |
| 215 |
|
| 216 |
.. literalinclude:: ../../example/tutorial/step3/main.js |
| 217 |
:language: js |
| 218 |
:start-at: function getWebSocketServer |
| 219 |
:end-before: function initGame |
| 220 |
|
| 221 |
Then, update the initialization to connect to this address instead: |
| 222 |
|
| 223 |
.. code-block:: javascript |
| 224 |
|
| 225 |
const websocket = new WebSocket(getWebSocketServer()); |
| 226 |
|
| 227 |
Commit your changes: |
| 228 |
|
| 229 |
.. code-block:: console |
| 230 |
|
| 231 |
$ git add . |
| 232 |
$ git commit -m "Configure WebSocket server address." |
| 233 |
[main 0903526] Configure WebSocket server address. |
| 234 |
1 file changed, 11 insertions(+), 1 deletion(-) |
| 235 |
$ git push |
| 236 |
... |
| 237 |
To github.com:python-websockets/websockets-tutorial.git |
| 238 |
+ 4a4b6e9...968eaaa main -> main |
| 239 |
|
| 240 |
Deploy the web application |
| 241 |
-------------------------- |
| 242 |
|
| 243 |
Go back to GitHub, open the Settings tab of the repository and select Pages in |
| 244 |
the menu. Select the main branch as source and click Save. GitHub tells you |
| 245 |
that your site is published. |
| 246 |
|
| 247 |
Open https://<your-username>.github.io/websockets-tutorial/ and start a game! |
| 248 |
|
| 249 |
Summary |
| 250 |
------- |
| 251 |
|
| 252 |
In this third part of the tutorial, you learned how to deploy a WebSocket |
| 253 |
application with Koyeb. |
| 254 |
|
| 255 |
You can start a Connect Four game, send the JOIN link to a friend, and play |
| 256 |
over the Internet! |
| 257 |
|
| 258 |
Congratulations for completing the tutorial. Enjoy building real-time web |
| 259 |
applications with websockets! |
| 260 |
|
| 261 |
Solution |
| 262 |
-------- |
| 263 |
|
| 264 |
.. literalinclude:: ../../example/tutorial/step3/app.py |
| 265 |
:caption: app.py |
| 266 |
:language: python |
| 267 |
:linenos: |
| 268 |
|
| 269 |
.. literalinclude:: ../../example/tutorial/step3/index.html |
| 270 |
:caption: index.html |
| 271 |
:language: html |
| 272 |
:linenos: |
| 273 |
|
| 274 |
.. literalinclude:: ../../example/tutorial/step3/main.js |
| 275 |
:caption: main.js |
| 276 |
:language: js |
| 277 |
:linenos: |
| 278 |
|
| 279 |
.. literalinclude:: ../../example/tutorial/step3/Procfile |
| 280 |
:caption: Procfile |
| 281 |
:language: text |
| 282 |
:linenos: |
| 283 |
|
| 284 |
.. literalinclude:: ../../example/tutorial/step3/requirements.txt |
| 285 |
:caption: requirements.txt |
| 286 |
:language: text |
| 287 |
:linenos: |