Firefly Simulator

Websockets, Multithreading, and Firefly Simulation
Time since last reset: 00:00:00

The Inspiration


Fireflies, also known as lightning bugs, are known for their ability to synchronize their flashing patterns. In some species, male fireflies gather in large numbers and flash in unison to attract females. This synchronization is a beautiful example of emergent behavior in nature, where simple individual actions lead to complex group patterns.

The Project


The FireFly Simulator is a web-based application that mimics this synchronization behavior. It's built using Go for the backend and HTML/JavaScript for the frontend, with real-time communication facilitated by WebSockets.

  • Real-time Simulation: The simulator updates in real-time, allowing users to watch as the fireflies gradually synchronize their flashing.
  • Customizable Grid: The simulation runs on a 30x30 grid, with each cell representing a potential firefly.
  • Neighbor Influence: Each firefly is influenced by its neighbors within a 10-cell radius, mimicking the way real fireflies respond to their environment.
  • Randomized Behavior: Fireflies have randomized cycle rates and flash durations, adding an element of unpredictability to the simulation.
  • Interactive Reset: Users can restart the simulation at any time with a click of a button.
  • Auto-Reset: The simulation automatically resets every hour to showcase different synchronization patterns.
  • Time Tracking: A timer displays how long the current simulation has been running.

The Technology


The backend is written in Go, leveraging its excellent concurrency features to manage the state of thousands of fireflies efficiently. Each firefly is represented as a goroutine, allowing for parallel processing of state updates.

The frontend uses vanilla JavaScript to render the grid and handle user interactions. WebSockets ensure that state updates are pushed to the client in real-time, creating a smooth and responsive simulation.

Try It Yourself!


The FireFly Simulator is open-source and available on GitHub. Feel free to clone the repository, run it locally, and experiment with different parameters. You can adjust the grid size, the number of active fireflies, or the influence radius to see how it affects the synchronization patterns.

Who knows? You might discover some interesting patterns that we haven't seen yet!

Conclusion


The FireFly Simulator is more than just a fun visualization - it's a testament to the beauty of natural phenomena and the power of modern web technologies to bring these wonders to life in our browsers. It reminds us that even in the digital age, we can find inspiration in the small, twinkling lights of summer evenings.

Liam Barter-Browning

Intermediate Software Developer, and Business Consultant.