> For the complete documentation index, see [llms.txt](https://docs.edgegap.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.edgegap.com/docs/sample-projects/web-games/gemini-bomb-tag.md).

# Gemini Bomb Tag

Make a Gemini Canvas game multiplayer with Edgegap: keep the rules between two marker lines, run them on a Node.js server near players, and add Find Match with the Edgegap matchmaker.

Learn how to take a browser game made in [Gemini](https://gemini.google.com/) Canvas online on Edgegap.

![Gemini Bomb Tag, running online matches on Edgegap](https://raw.githubusercontent.com/edgegap/netcode-sample-gemini-bomb-tag/main/docs/images/card.jpg)

Bomb Tag is an arena game for 4 runners: one holds a bomb with a 12-second fuse, touching a rival passes it on, and whoever holds it when the fuse runs out is out. Last runner standing wins. Gemini wrote the whole game, solo against 3 bots, as one HTML file in Canvas.

* **Gemini**: used while you build the game, to write the rules, the renderer and the bots.
* **Edgegap**: used while people play, for one dedicated server per match, near its players, and optional matchmaking.

This sample shows:

* **Rules between two marker lines**: the prompt asks Gemini to keep every game rule between `/* RULES:BEGIN */` and `/* RULES:END */`, with no browser code inside. The file still plays in Canvas, and you can keep changing it in the chat.
* **One script for the server**: `tools/split-artifact.mjs` copies that block into `client/rules.js`, which the Node.js server runs. It refuses browser-only code in the block, and a CI check keeps the server's rules in sync with the file.
* **Two ways to play online**: join one server by its address (`?server=`), or click **Find match** with the [Edgegap JavaScript SDK](https://www.npmjs.com/package/@edgegap/sdk), 2 to 4 players per match, with bots in the empty seats.
* **Gemini's own output**: the tag `before` in the repository holds the file as Gemini wrote it, so every change for Edgegap is visible in the history.

{% hint style="info" %}
See the [sample repository](https://github.com/edgegap/netcode-sample-gemini-bomb-tag) for the game, the split script, the game server, the matchmaker config and the tests.
{% endhint %}

## ✔️ Preparation

Before we start, you will need:

* [Gemini Bomb Tag sample](https://github.com/edgegap/netcode-sample-gemini-bomb-tag), or your own game made in Gemini Canvas.
* [Node.js](https://nodejs.org/) 22 or newer, to run the split script, the server and the tests.
* A GitHub repository, so GitHub Actions can build the server image. [Docker](https://docs.docker.com/get-docker/) works too.

☑️ Try the game locally first:

{% code collapsedlinecount="10" %}

```bash
npm install
npm run server
npm run client
```

{% endcode %}

Run the last two commands in separate terminals. Open `http://localhost:8080` in two browser tabs and click **Local server :7777** in both.

## 🤖 Build the Game with Gemini

### 1. Ask for Rules Between Markers

☑️ In Gemini, click **+**, choose **Canvas**, describe your game and keep the architecture part of this prompt as it is. Bomb Tag came from this prompt:

{% code overflow="wrap" collapsedlinecount="10" %}

```
Build a small browser game as a single HTML file called "Bomb Tag" (plain Canvas 2D, no frameworks, no external libraries) and show it in Canvas so I can play it in the preview. Gameplay: 4 round runners in a square arena (800x800 units) move with WASD/arrow keys with drift (velocity + friction). One random runner holds a bomb with a 12-second fuse. Touching another runner passes the bomb to them (the new holder can't pass it back for 0.6 s). When the fuse runs out, the holder explodes and is out; the bomb goes to a random remaining runner with a fresh fuse. Last runner standing wins, then a results screen and restart. The human plays against 3 simple bots. VERY IMPORTANT architecture: put ALL game rules in one block between the exact lines /* RULES:BEGIN */ and /* RULES:END */, with NO window, document, canvas, audio, requestAnimationFrame or timers inside, and no export keyword. The world is plain data (objects, arrays, numbers, strings; no classes or Maps). The block defines createWorld() that returns a world with no players, addPlayer(world, id, name, isBot) that places each new player at its own free start spot (up to 4 players, any ids), removePlayer(world, id), setInput(world, id, {up,down,left,right}) that works for every player, step(world, dt) that moves bot players itself with botInput() and returns an array of events ({type:'pass', from, to}, {type:'boom', id} and {type:'win', winner}), and botInput(world, id) using only positions and who holds the bomb. Everything else (rendering, keyboard input, calling step() at 60 Hz) stays outside the block.
```

{% endcode %}

The architecture part also asks for what a server needs: no players in `createWorld()`, a start spot per `addPlayer()`, `setInput()` for every player, and bots moved inside `step()`. Gemini's first answer had all four.

☑️ Play the game in the Canvas preview and check that the bots move and the fuse counts down.

☑️ In the Canvas toolbar, click **Export**, then **Download code**, and save the file unchanged as `artifact/bomb-tag.html` in your repository.

{% hint style="warning" %}
**Pick a game that holds up with network delay.** Drifting movement, pushing, tagging, slow projectiles and turn-based play feel right online. Precise platforming, rhythm games and instant-hit aiming need more than this sample does.
{% endhint %}

✅ You may now proceed to the next step.

### 2. Split Out the Rules

☑️ Copy `tools/split-artifact.mjs` from the sample and run it once:

{% code overflow="wrap" collapsedlinecount="10" %}

```bash
node tools/split-artifact.mjs artifact/bomb-tag.html --page
```

{% endcode %}

It writes `client/rules.js`, the block plus an `export` of every function in it, and `client/index.html`, Gemini's page loading those rules. If the block uses browser-only code, the script names the line: send that line back to Gemini and ask to move it outside the block.

When you change the game later in Gemini, save the new file over the old one and run `npm run split`.

✅ You may now proceed to the next step.

### 3. Add the Server and Online Play

☑️ Copy `server/`, `client/online.js`, `client/net.js`, `client/config.js` and `tests/` from the sample. The only server file written for this game is `server/game.js`: it seats players in random order, runs the rules at 60 Hz (they move the bots), accepts the four direction buttons from each player, ranks players by when they went out, and sends the world back 20 times a second.

☑️ Add the online part of the sample's `client/index.html` to your page: **Find match** and **Local server** buttons, drawing the server's world with Gemini's own renderer, and the waiting and countdown on Gemini's overlay.

☑️ Run `npm run test:local`: two headless players play a full match against a local server.

✅ You may now proceed to the next step.

## ⚡ Deploy and Connect

### 4. Build the Server Image

☑️ To get started, you'll need to [create a free account with Edgegap](https://app.edgegap.com/auth/register). No credit card required.

☑️ Find your [Container Registry](https://docs.edgegap.com/learn/advanced-features/edgegap-container-registry.md) project name and credentials. In your GitHub repository, add the variable `EDGEGAP_REGISTRY_PROJECT` and the secrets `EDGEGAP_REGISTRY_USERNAME` and `EDGEGAP_REGISTRY_TOKEN`, then copy the sample's `.github/workflows/server-image.yml`.

Every push to `main` checks that the server's rules match the file, plays a test match, and pushes `registry.edgegap.com/<project>/bomb-tag:<tag>`.

{% hint style="warning" %}
Keep `provenance: false` and `sbom: false` in the workflow. With the defaults, the image is wrapped in an index that Edgegap cannot pull.
{% endhint %}

✅ You may now proceed to the next step.

### 5. Create the App Version

☑️ [Create an application](https://app.edgegap.com/application-management/applications/list) named `bomb-tag`, with one version named after your tag:

* **Image**: registry `registry.edgegap.com`, repository `<project>/bomb-tag`, tag `<tag>`,
* **Resources**: 0.25 vCPU and 512 MB memory,
* **Port**: `7777`, name `gameport`, protocol **WS**, with **TLS Upgrade** enabled,
* **Restart policy**: Never,
* **Max duration**: 30 minutes, as a safety net: the server stops itself,
* **Environment variables**: `EMPTY_TIMEOUT_S=15`, `FIRST_JOIN_TIMEOUT_S=60`, `RESULTS_SECONDS=8`.

{% hint style="warning" %}
**TLS Upgrade** lets browsers on `https` pages connect to `wss://<id>.pr.edgegap.net:<port>`. Edgegap terminates TLS and forwards plain WebSocket traffic to port `7777`. Connect with the host name, not the IP, so the certificate matches.
{% endhint %}

{% hint style="info" %}
Free Tier is limited to 2 Applications, 2 versions, and 10 GB of Container Registry storage.
{% endhint %}

☑️ Deploy one server from the dashboard, then open the page in two tabs with the deployment's address:

{% code overflow="wrap" collapsedlinecount="10" %}

```
http://localhost:8080/?server=wss://<fqdn>:<external port>
```

{% endcode %}

🙌 Both players are now in the same match, on a server placed near them, with bots in the empty seats. When the match ends, the deployment stops itself.

✅ You may now proceed to the next step.

### 6. Add Find Match (Optional)

☑️ Set `application.version` in the sample's `matchmaker/bomb-tag.json` to your version, and add the address of your page to `allowed_cors_origins`.

☑️ Open the [Matchmaker](https://app.edgegap.com/matchmaker-management-v2/matchmakers/list) page in the dashboard, click **Create**, upload the config, and click **Create and Start**.

The `bomb-tag` profile matches 2 to 4 players. After 20 seconds, a player waiting alone gets a server, and bots fill the other seats. Each game gets its own profile, so several games can share one matchmaker.

☑️ When the matchmaker shows **Online**, copy its **API URL** and **Auth Token** into `client/config.local.js`, a copy of `client/config.js`.

{% hint style="success" %}
The **matchmaker Auth Token is safe to ship in web pages**: it can only ask for matches. Never put your Edgegap API token in a page or in the server image.
{% endhint %}

☑️ Open your page in two browser tabs and click **Find match** in both within 20 seconds.

Each page:

1. joins the matchmaker and waits for `HOST_ASSIGNED`,
2. connects to `wss://<fqdn>:<port>`, retrying for a few seconds while the server starts,
3. plays: it sends its four buttons and draws the world the server sends back.

The game server:

1. waits for every matched player, then fills the empty seats with bots,
2. runs the match with the same rules as Gemini's file,
3. stops its own deployment after the results screen, or soon after everyone leaves.

## 📦 Ship Your Game

* Host the page anywhere: `client/` is static files with no build step. The sample includes a GitHub Pages workflow.
* Keep making the game in Gemini: ask for the change in the same Canvas while keeping every rule between the markers, download the code again, then run `npm run split`.
* To ship new rules, push them, and point the app version at the new image tag. New matches use it, and running ones keep the old build.
* Turn `inspect` off in the matchmaker config before launch.

{% hint style="info" %}
Using a coding agent? The `edgegap-chat-canvas` skill in [Edgegap's agent skills](https://github.com/edgegap/llm-agent-skills), also served by the [Edgegap MCP server](https://docs.edgegap.com/docs/mcp-integration.md), holds these steps, the split script and the tests, and walks your agent through the same path for your own Gemini Canvas game.
{% endhint %}
