Skip to content

Fly.io

Fly.io supports WebSockets and provides automatic HTTPS—a good fit for PyView apps.

Prerequisites

  1. Create a Fly.io account
  2. Install the Fly CLI:
Terminal window
# macOS
brew install flyctl
# Linux
curl -L https://fly.io/install.sh | sh
  1. Log in:
Terminal window
fly auth login

Initial Setup

From your project directory:

Terminal window
fly launch

This creates a fly.toml configuration file. When prompted:

  • Choose a unique app name (or accept the generated one)
  • Select a region close to your users
  • Skip database setup unless you need it

Configuration

Here’s a complete fly.toml for a PyView app:

app = "your-app-name"
primary_region = "ewr" # Change to your preferred region
[build]
# Uses Dockerfile in the current directory
[env]
# Non-secret environment variables
PORT = "8000"
[http_service]
internal_port = 8000
force_https = true
auto_stop_machines = false # Keep alive for WebSocket connections
auto_start_machines = true
min_machines_running = 1
[http_service.concurrency]
type = "connections"
hard_limit = 100
soft_limit = 50
[[vm]]
size = "shared-cpu-1x"
memory = "256mb"

Key settings:

  • force_https = true — Redirects HTTP to HTTPS (recommended)
  • auto_stop_machines = false — Keeps machines running for WebSocket connections
  • internal_port = 8000 — Matches uvicorn’s default port

Setting Secrets

Terminal window
fly secrets set PYVIEW_SECRET="$(openssl rand -base64 32)"

Secrets are encrypted and injected as environment variables. List them with:

Terminal window
fly secrets list

Deploying

Terminal window
fly deploy

Fly builds your Docker image remotely and deploys it. Once deployed:

Terminal window
fly open

Monitoring

View logs in real-time:

Terminal window
fly logs

Check app status:

Terminal window
fly status

SSH into a running machine for debugging:

Terminal window
fly ssh console

Scaling

Regions

Terminal window
fly regions add lax ord # Los Angeles, Chicago

Machine Size

Terminal window
fly scale vm shared-cpu-2x --memory 512

Or update fly.toml:

[[vm]]
size = "shared-cpu-2x"
memory = "512mb"

Multiple Machines

Terminal window
fly scale count 2

Fly’s load balancer uses sticky sessions by default, which works well with PyView’s WebSocket connections.

Custom Domain

Terminal window
fly certs add your-domain.com

Follow the DNS instructions provided. Fly handles TLS certificates automatically.

Troubleshooting

App won’t start

Check the logs:

Terminal window
fly logs

Common issues:

  • Missing PYVIEW_SECRET — Set it with fly secrets set
  • Wrong port — Ensure internal_port matches your uvicorn port
  • Build failures — Check Dockerfile syntax

WebSocket not connecting

  • Verify HTTPS is working (WebSocket requires secure connection in production)
  • Check that auto_stop_machines = false if using aggressive auto-stop

Slow cold starts

If your app stops and restarts frequently:

[http_service]
auto_stop_machines = false
min_machines_running = 1

Example: PyView Demo

The PyView examples run on Fly.io. See the fly.toml in the repository for a working configuration.