All posts

Cloudflare

I turned my portfolio into a town square you walk around with the other visitors

By Vivek Bhange5 min read

Cover image for I turned my portfolio into a town square you walk around with the other visitors

Most portfolios are a page you read alone. I wanted mine to feel like a place, so I built Vivek’s Square: a small 3D town square where everyone on the site at the same moment shows up as a little character. You walk around, wave, say a few set phrases, and if you tap someone you can challenge them to a duel. Accept, and you’re both dropped into the same online match.

It runs on phones held upright, needs no sign-up, and costs close to nothing on a quiet day. This is how it’s put together.

The shape of it

  • The page is three.js, written by hand: a paved square, a banyan tree on a stone platform, bunting, lamp posts, and a ring of buildings. Every building is a door to something already on the site: the arcade, the blog, my projects, the guestbook.
  • The server is a Cloudflare Durable Object called Plaza, one per square. A square holds up to 50 people. When it fills, the lobby sends newcomers to square 2, and so on, so every square feels lively and none is a crowd.
  • The lobby lives in another Durable Object that already ran my site’s “here now” counter, reactions and guestbook. Each square reports how full it is; the lobby hands out the first square with room.

Everything else (static pages, the games) is plain files on Cloudflare’s edge. Only /api/* runs code.

Keeping the traffic small

The obvious version sends every player’s position to every other player, many times a second. With 50 people that’s 2,500 messages per tick. Instead:

  1. A phone sends its position at most 10 times a second, and only while it’s moving. Standing still costs nothing.
  2. The square collects everyone’s moves and sends one bundled message every 100 ms to each person.
  3. Each phone draws other people about 160 ms in the past, blending between the last two positions it has, so walking looks smooth even when messages arrive unevenly.

Talking is six emotes and twelve set phrases. No free text, so there’s nothing to moderate. Names go through the same filter my arcade leaderboards use.

Making it run on a cheap phone

A town square has a lot of small things in it: bricks, flags, lamp posts, trees. Drawn one by one, that’s hundreds of draw calls, and a mid-range phone starts dropping frames. So everything that never moves is baked into a single mesh at load time, with each part’s colour written into the vertices. The whole square, bunting included, is one draw call. The signs are the only textured pieces.

The characters get the same treatment: each look (shirt colour, hat, skin tone) is baked once into one mesh and shared, so a person is one draw call plus their name tag. Everyone’s soft round shadow is drawn together, in one instanced mesh.

Dueling a stranger

My arcade already had three online duels (Rooftop Duel, Kabaddi Dash and Slime Volley) where one player hosts a room and the other types a five-letter code. The square just removes the typing:

  1. When you challenge someone, your phone makes up a room code and sends it with the challenge, through the square, to that one person.
  2. If they accept, you both leave the square: you open the duel as the host of that code, they open it as the guest.
  3. The match starts the moment both are in. If the guest’s page loads first, it quietly retries for a few seconds until the host’s room exists.

The host’s device runs the match and streams snapshots to the guest. Phone networks deliver in bursts, so the guest now keeps a small buffer of snapshots stamped with the host’s clock, and plays them back a little behind, at a delay that grows on a shaky link and shrinks on a good one. On a simulated phone network with regular stalls, that took the guest’s picture from 250 frozen frames and 95 backward jerks in 18 seconds to none, for about 25 ms of extra delay.

A daily puzzle, built backwards

Next to the square there’s Barish (Hindi for rain): one puzzle a day for everyone. Rain falls from clouds onto slanted roof tiles; you flip tiles so every field gets water and no house gets wet.

Each day starts from a solved board. The builder flips a few tiles to make the puzzle, then searches every shorter set of flips (only tiles a raindrop actually touches can matter, which keeps the search small) and throws the board away if any of them solves it. So the par is exact: “par 4” really means four flips is the fewest. The hardest days take up to a second to build, too slow for a phone, so a year of puzzles is built when the site is built and the page just reads today’s.

Bugs that taught me something

  • The lobby forgot everything. Square counts were kept in memory in the lobby’s Durable Object. That object also holds the site’s “here now” WebSockets with the hibernation API, which means Cloudflare can put it to sleep between requests, and sleeping wipes memory. Counts now live in its SQLite storage. Rule I’m keeping: if a Durable Object hibernates, nothing important lives in a variable.
  • requestAnimationFrame time can be older than the click. The rain animation started from performance.now() in the click handler, and the next frame’s timestamp was a few milliseconds earlier. Negative elapsed time, an index of -1, and the animation silently stopped. Clamp at zero.
  • “Right” wasn’t right. In my battle royale, pushing the joystick right walked you left, on keyboard too. With the camera looking along +Z, screen-right is −X; the movement code had it the other way. I now derive camera-relative directions from the camera’s own vectors instead of from memory.

What it costs

Very little while it’s quiet: a square only works while people are in it, and idle sockets hibernate. The cap of 50 per square and “only send while moving” keep a busy day bounded. I’ll share real numbers once it has seen some traffic.

Try it

Open the square on your phone, send the link to a friend, and challenge them to a duel. If nobody’s there yet, stay a minute: whoever walks in next will see you standing by the banyan tree.

Enjoyed this?

Written by Vivek Bhange, Full-Stack AI Engineer. See his work or work with me.

Keep reading

Easter egg hunt

This site hides 16 surprises. Found ones stay found on this device. On a phone, type secret words into the command bar (the >_ button).

  1. RickrolledNot found yet

    Show hint

    My photo has a secret. Double-click it (on a phone, press and hold).

  2. Impatient clickerNot found yet

    Show hint

    Click anywhere, fast, about 25 times in a row.

  3. Agent of changeNot found yet

    Show hint

    Type the word that means "make it different".

  4. Gravity checkNot found yet

    Show hint

    Type my first name.

  5. Secret wordNot found yet

    Show hint

    Type something friendly, like "chai" or "namaste".

  6. HackerNot found yet

    Show hint

    Type what hackers do (four letters).

  7. The snapNot found yet

    Show hint

    Type the name of a purple villain who snaps.

  8. Sharp eyesNot found yet

    Show hint

    Something almost invisible floats on the screen. Find it and click it (desktop).

  9. Root accessNot found yet

    Show hint

    Open the command bar (press /, or tap >_ on a phone), then try to become root.

  10. Duck hunterNot found yet

    Show hint

    A rubber duck is hiding on the 3D island.

  11. World travellerNot found yet

    Show hint

    Visit all four views.

  12. Night owlNot found yet

    Show hint

    Come back between midnight and 4 a.m.

  13. Arcade regularNot found yet

    Show hint

    Play every quick game in the Arcade (the ones that open on the Arcade page).

  14. Chain reactorNot found yet

    Show hint

    Set off 30 bursts with a single move in Critical Mass.

  15. Last one standingNot found yet

    Show hint

    Win a match of Drop Zone.

  16. Code whizNot found yet

    Show hint

    Get 10 answers right in a row in Code Clash.

zsh — visitor@vivek: ~

Ask Vivek

Ask me anything. My work, how I run projects, or whether I'm a fit.

AI answers from this site's content. It can make mistakes.