Annex · for grown-ups 🛠️
Annex

Build & host the game

The technical companion to the notebook: how to build the bundle, wrap it in Docker, and deploy it — on alwaysdata, a VPS, a container platform, or plain static hosting.

?First, pick what you're shipping

Tower Duel can be deployed in two very different shapes. Choosing the right one decides everything else on this page.

Static (solo) dist/ files any web host just files — no Node, no server ✗ no TV + phones mode Node server (connected) TV + phones Node + WebSocket authoritative simulation on the server ✓ full multiscreen experience
Left: a folder of static files. Right: a small Node process that serves the page and referees the realtime match.
Key decision. Want phones-as-controllers? You need the Node server (Docker, VPS, alwaysdata, Fly.io…). Just the solo keyboard game? Static hosting is enough.

1Build the bundle

Everything starts from one command. Vite compiles the source into a self-contained dist/ folder of HTML, JS and assets.

build the production bundle
# install exactly the locked dependencies
npm ci
# compile the single-page bundle into dist/
npm run build

The Vite config uses base: './' (relative paths), so the result is portable: it works whether it's served from a domain root or a sub-path. To preview the built bundle locally:

preview / serve locally
# static preview (solo only)
npm run preview
# full Node server (page + realtime), on http://localhost:3000
npm start
Remember. npm start runs node server/index.js and serves dist/, so always npm run build first.

2Wrap it in Docker

The repository ships a multi-stage Dockerfile. A first stage installs everything and builds the bundle; a second, slim stage keeps only the runtime dependencies and the built files. Smaller image, no build tools in production.

Dockerfile (shape)
# --- build stage: compile with Vite ---
FROM node:24-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# --- runtime stage: Node (SPA + WebSocket rooms) ---
FROM node:24-alpine
WORKDIR /app
ENV NODE_ENV=production
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
EXPOSE 3000
CMD ["node", "server/index.js"]

Build the image, then run it:

build & run
# build the image (the trailing dot = current folder)
docker build -t tower-duel .
# run it: map host port 8088 to the container's 3000
docker run --rm -p 8088:3000 -e PUBLIC_HOST=192.168.1.20 tower-duel

Open http://localhost:8088. For phones to join they need a reachable address — but inside a container the auto-detected IP is the Docker bridge (e.g. 172.17.0.x), which phones can't reach. Set PUBLIC_HOST to the machine's LAN address (e.g. 192.168.1.20) so the QR code on the TV points somewhere reachable.

For the curious. The server reads the PORT (default 3000) and the optional PUBLIC_HOST (the host/IP advertised in the QR code) environment variables. The image also declares a HEALTHCHECK that pings /, so orchestrators can tell when the container is actually ready.

3On a VPS (Docker + reverse proxy)

On your own server (a $5 VPS is plenty), run the container and put nginx in front to terminate HTTPS and forward traffic. The one thing you must not forget: WebSocket upgrade headers, or the realtime channel silently fails.

/etc/nginx/sites-available/towerduel
server {
  listen 80;
  server_name towerduel.example.com;

  location / {
    proxy_pass http://127.0.0.1:8088;
    proxy_http_version 1.1;
    # these two lines are what make WebSocket work
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
  }
}

Then get a free HTTPS certificate (the browser requires wss:// on an HTTPS page):

HTTPS in one command
sudo certbot --nginx -d towerduel.example.com
Pitfall. On an https:// page the WebSocket must be wss:// (secure). The client builds the URL from the current page, so serving the whole site over HTTPS is enough — no code change needed. Mixing http page + wss (or the reverse) is blocked by the browser.

4Deploy on alwaysdata

alwaysdata runs a Node.js application directly, without Docker. The server serves the page and the WebSocket on the same origin — so there's nothing special to configure on the realtime side, and no client change (it builds wss://<host>/ws on its own from the page). No need to set up two separate sites like some tutorials do: a single Node site at the root is enough.

1) Get the code there (the simplest: the release bundle)

No need to build anything, neither locally nor on the server. Pull the pre-built bundle, published with every release, straight onto the server: the game is already compiled, so alwaysdata only needs to install express + ws. The URL is permanent and always serves the current version.

download the release bundle, then install it on alwaysdata
# over SSH on alwaysdata, go to your working directory
cd ~/www/towerduel
# download the release bundle (permanent URL → current version)
wget https://github.com/1e1/tower-phaser/releases/latest/download/tower-duel-server.zip
unzip tower-duel-server.zip   # the game is already compiled
# install express + ws only
npm ci --omit=dev          # NO vite, NO build
Alternative: your own build. To deploy a local change not yet published as a release, run npm run package locally (build + zip into release/), upload the contents of release/tower-duel-<version>/ into your working directory, then carry on with the same npm ci --omit=dev.
⚠️ Pitfall: on alwaysdata the install runs in production mode by default, so npm install / npm ci omits the devDependencies — including vite. If you deploy the raw git repo (without a pre-built dist/) and try npm run build in place, it fails for lack of vite. That's exactly what the release bundle avoids: it already ships dist/. Failing that, force npm ci --include=dev && npm run build to build on the server.

2) Create the site (Web → Sites → Add a site)

In the admin panel, add a site and fill in:

Port: alwaysdata provides the PORT variable and the server already reads it — nothing to change. Domain & HTTPS: attach your (sub)domain, alwaysdata issues the certificate → page and WebSocket served over HTTPS/WSS automatically.

Why it's simple here. A single Node process answers both the pages and /ws, so no separate WebSocket service and no custom proxy. But: alproxy cuts idle connections after 300 s, and rooms live in memory inside that process. The server sends a heartbeat every 30 s to keep lobby sockets alive; and as long as you stay on a single instance, everyone lands on the same room registry. If the site grows and moves to several processes/instances, two players on different instances will no longer find each other (“room not found”) — you'll then need shared state (Redis…) or sticky sessions.

5Container platforms (Fly.io, Render…)

Managed container hosts deploy the exact same Docker image, with WebSocket support out of the box.

For the curious. The match lives in memory (no database). That's fine for a game, but it means each instance holds its own rooms — keep it to a single instance, or two players who land on different instances won't find each other.

6Static-only hosting (GitHub Pages, Netlify)

If you only want the solo, keyboard game, you can host the built files anywhere static — no server bill at all.

deploy the static bundle
npm run build
# then publish the contents of dist/ to your static host
# (Netlify: drag & drop dist/, or set build = "npm run build", publish = "dist")
Limitation. Static hosting has no Node process, so the connected TV + phones mode cannot run. The QR/room flow needs the server. For multiscreen, use one of the Node options above.
Note. This very notebook is published with GitHub Pages from the docs/ folder — that's static hosting in action, for documentation rather than the game.

✓Troubleshooting checklist

↩ Back to home

← Chapter 4 Build & host