Hosting game servers on demand (when another service asks for it)

This’ll work well with Fly Machines and a feature we call fly-replay.

What most people do is have one “gateway” app that accepts all requests, and then one or more other apps that contain the machines they expose to end users. Since you’re talking about sending people to specific machines, I’d put all the game servers in a second app. So you’ll end up with:

  • game-gateway
  • game-server

When you receive a request in the gateway, you do all your work to figure out which machine it should be routed to. Or create a machine, then keep the ID.

Once you know the ID you want the request to go to, just reply with a header like fly-replay: app=game-server;instance=<id>. This will result in the websocket being connected to the server you want. If the machine is stopped, our proxy will even start it for you if it’s in a stopped state.

Killing off old machines is a little more work. The simplest is probably to make them exit when they’re finished, and then poll the API from your gateway app periodically to find stopped machines, then remove them.

Does that make sense?

4 Likes