{"id":13443077,"url":"https://github.com/fly-apps/tictac","last_synced_at":"2025-07-05T19:37:35.974Z","repository":{"id":44409564,"uuid":"349777864","full_name":"fly-apps/tictac","owner":"fly-apps","description":"Demonstration of building a clustered, distributed, multi-player, turn-based game server written in Elixir.","archived":false,"fork":false,"pushed_at":"2021-11-01T21:33:57.000Z","size":1606,"stargazers_count":364,"open_issues_count":1,"forks_count":29,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-07-05T19:37:33.382Z","etag":null,"topics":["elixir","game","global","liveview","multi-player"],"latest_commit_sha":null,"homepage":"https://fly.io/blog/building-a-distributed-turn-based-game-system-in-elixir/","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fly-apps.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-20T16:27:52.000Z","updated_at":"2025-06-23T16:10:26.000Z","dependencies_parsed_at":"2022-07-16T18:30:42.153Z","dependency_job_id":null,"html_url":"https://github.com/fly-apps/tictac","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fly-apps/tictac","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fly-apps%2Ftictac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fly-apps%2Ftictac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fly-apps%2Ftictac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fly-apps%2Ftictac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fly-apps","download_url":"https://codeload.github.com/fly-apps/tictac/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fly-apps%2Ftictac/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263795389,"owners_count":23512671,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","game","global","liveview","multi-player"],"created_at":"2024-07-31T03:01:55.735Z","updated_at":"2025-07-05T19:37:35.914Z","avatar_url":"https://github.com/fly-apps.png","language":"Elixir","funding_links":[],"categories":["Elixir"],"sub_categories":[],"readme":"# Tictac\n\nThis is a demonstration of building a clustered, distributed, multi-player, turn-based game server written in Elixir. As designed, it plays Tic-Tac-Toe, but was designed to be extended to play almost any multi-player turn based game.\n\nThis uses Phoenix LiveView for the UI, TailwindCSS for styles, `libcluster` for clustering the nodes, `horde` for providing a distributed process registry, and [fly.io](https://fly.io) for hosting and multi-region clustering support.\n\nYou can read the blog post about it here: [fly.io/blog/building-a-distributed-turn-based-game-system-in-elixir/](https://fly.io/blog/building-a-distributed-turn-based-game-system-in-elixir/)\n\n## Try it out locally\n\nTo try the project out locally:\n\n  * Install dependencies with `mix deps.get`\n  * Install Node.js dependencies with `npm install` inside the `assets` directory\n  * Start Phoenix endpoint with `mix phx.server`\n\nNow you can visit [`localhost:4000`](http://localhost:4000) from your browser.\n\nSince ths is multi-player, open a second browser window to the same address. [`localhost:4000`](http://localhost:4000)\n\nThis is what a game looks like:\n\n![Local Game Example](images/Local_playing.gif)\n\n## Running it multi-node and clustered\n\nTo run it clustered locally, in a terminal window, run the following command:\n\n```\nPORT=4000 iex --name a@127.0.0.1 --cookie asdf -S mix phx.server\n```\n\nIn a separate terminal window, run this command:\n\n```\nPORT=4001 iex --name b@127.0.0.1 --cookie asdf -S mix phx.server\n```\n\nNow in one browser window, visit [`localhost:4000`](http://localhost:4000).\n\nFrom another browser window, visit [`localhost:4001`](http://localhost:4001).\n\nYou created two clients that are connected to two separate Elixir nodes which are clustered together! This is what it looks like where \"ABCD\" is a started game.\n\n![Multi-node local machine](images/home-computer-multi-node.png)\n\n## Deploying it to Fly.io\n\nDeploy this to your own Fly.io account and see it in action for yourself!\n\n- Setup your [Fly.io account](https://fly.io/docs/hands-on/start/)\n- Clone this repo\n- Register your app on Fly.io\n\n```\nfly launch --name my-special-custom-name\n```\n\n- Take all the defaults. As for the region, choose `sea` (Seattle, Washington (US))\n- Replace the generated `fly.toml` file with the following config but keep the `app` name that you chose for your app.\n\n```yaml\napp = \"\u003cmy-special-custom-name\u003e\"\n\nkill_signal = \"SIGTERM\"\nkill_timeout = 5\n\n[[services]]\n  internal_port = 4000\n  protocol = \"tcp\"\n\n  [services.concurrency]\n    hard_limit = 25\n    soft_limit = 20\n\n  [[services.ports]]\n    handlers = [\"http\"]\n    port = 80\n\n  [[services.ports]]\n    handlers = [\"tls\", \"http\"]\n    port = 443\n\n  [[services.tcp_checks]]\n    grace_period = \"30s\" # allow some time for startup\n    interval = \"15s\"\n    restart_limit = 6\n    timeout = \"2s\"\n```\n\n- Setup your Phoenix secrets\n\n```\n$ mix phx.gen.secret\ngZNW554LeBx4VGuG2U+X3fe7OCmk28g3OIE0Ia+OFRxS7+bhEm/2ZnIvnGTRo4DO\n\n$ fly secrets set SECRET_KEY_BASE=gZNW554LeBx4VGuG2U+X3fe7OCmk28g3OIE0Ia+OFRxS7+bhEm/2ZnIvnGTRo4DO\nSecrets are staged for the first deployment\n```\n\n- Deploy!\n\n```\nfly deploy\n```\n\n- Open it in the browser\n\n```\nfly open\n```\n\nAt this point you have a working system. This is where most systems stop!\n\n## Take it Multi-Region!\n\nReady to take it to multiple regions? We've got one in Seattle on the West Coast, let's add one on the East Coast to cover the whole US.\n\n- Add a region `ewr` (Parsippany, NJ (US))\n\n```\nfly regions add ewr\n```\n\n- Scale it up\n\n```\nfly scale count 2\n```\n\n- Check the Status - Re-run this command to see it balance out\n\n```\nfly status\n```\n\n- Check out the logs\n\n```\nfly logs\n\n2021-03-31T14:28:22.880Z c9b72c04 sea [info] [info] [libcluster:fly6pn] connected to :\"tictac@fdaa:0:1da8:a7b:ab3:1c48:eb59:2\"\n2021-03-31T14:28:22.881Z c9b72c04 sea [info] [info] Starting Horde.RegistryImpl with name Tictac.GameRegistry\n2021-03-31T14:28:22.884Z c9b72c04 sea [info] [info] Starting Horde.DynamicSupervisorImpl with name Tictac.DistributedSupervisor\n2021-03-31T14:28:22.890Z c9b72c04 sea [info] [info] Running TictacWeb.Endpoint with cowboy 2.8.0 at :::4000 (http)\n2021-03-31T14:28:22.892Z c9b72c04 sea [info] [info] Access TictacWeb.Endpoint at https://tictac.fly.dev\n```\n\nThe first log line shows the nodes are connected.\n\nYou now have a clustered Elixir application where users connect to the nearest server for them. This can provide a better user experience!\n\n![Fly region cluster](images/fly-region-cluster-eu.png)\n\n## What will you build?\n\nTic-Tac-Toe is a simple game. This architecture could support any multi-player turn-based game you might think of. What cool game do you want to make?\n\n## Livebook notebooks!\n\nI used [Livebook](https://github.com/elixir-nx/livebook) notebooks to document the app logic in a regular Phoenix Web application! I created two notebooks to document how the game state works and also how a game server runs that state.\n\nThey are just plain markdown files as far as the project is concerned. Open them with Livebook and follow [this guide](https://fly.io/blog/livebook-for-app-documentation/) to execute them in the context of the application.\n\nYou will find them in the `notebook` folder.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffly-apps%2Ftictac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffly-apps%2Ftictac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffly-apps%2Ftictac/lists"}