Backend
Spring Ant Farm
An ant colony simulation where the server runs the colony and the browser just watches. I'm building it to learn Spring Boot properly, with something on screen that moves when the backend does.
Role
Solo developer. A personal project for learning Spring Boot.
Stack
Why an ant farm
I wanted to learn Spring Boot on something more interesting than a to-do list API. A simulation gives the backend a real job: it has to keep state, update it on a schedule, and push changes out to whoever is watching.
Ants are a good fit for that. Each one is simple, there are a lot of them, and you can tell right away if something is broken because they stop moving or all pile up in a corner.
How it works right now
The colony lives on the server. The browser never decides where an ant goes. It only draws what the server tells it.
- The colony: a service holds a list of ants on a 100 by 100 grid. The number of ants comes from a config property, so changing the colony size does not mean touching code.
- The clock: a scheduled task ticks every 200 milliseconds and moves each ant one step in a random direction, clamped so nobody walks off the edge.
- The stream: every tick, the server sends the full list of ants to every open browser tab over Server-Sent Events. A plain endpoint also returns the current state once, for anything that just wants a snapshot.
- The view: a single HTML page opens the stream and redraws a canvas each time a new update arrives.
If you open the page in two tabs, both show the same colony, because there is only one colony and it belongs to the server.
What I'm learning from it
Most of the value so far has been seeing how Spring wires things together. The service, the broadcaster, and the controller never create each other. Spring builds each one and hands it the pieces it needs through its constructor.
Streaming taught me about connections that go away without saying goodbye. When someone closes a tab, the server only finds out the next time it tries to send to them. The broadcaster keeps a thread-safe list of viewers and drops anyone whose send fails, so a closed tab does not break the update for everyone else.
Current status
Right now the ants just wander. Nothing tells them where food is or how to get home, so the colony looks more like a crowd than a colony.
This page will grow as the project does.
