We've written a Python file that runs a HTTP server and a UDP server both on the same port (8080
).
For UDP server on fly.io, there are two important things to note:
- We need to bind it to
fly-global-services
, NOT localhost. See line 50 in the main.py file. - We need to allocate a dedicated ipv4 address for the app.
-
I've created a fly.toml file in the root directory with the following content:
[http_service] internal_port = 8080 force_https = true auto_stop_machines = 'suspend' auto_start_machines = true min_machines_running = 1 processes = ['app'] [[services]] protocol = 'udp' internal_port = 8080 [[services.ports]] port = 8080
-
Launch the app on Fly.io
fly launch --org sahil-manocha-115
Note:
fly launch
= Create a new app on Fly.io. Usefly deploy
to update an existing app. -
It will ask
An existing fly.toml file was found ? Would you like to copy its configuration to the new app? (y/N)
Type
y
and press enter. -
It will ask for app's configuration (region, name, etc.). Then it will build the image & push it to the registry.
-
IMPORTANT: Then it will ask if we want to allocate a dedicated ipv4 address for the app. SAY YES.
? Would you like to allocate a dedicated ipv4 address now? Y
-
Once it's done, it will show the app's URL.
Visit your newly deployed app at https://fly-http-and-udp.fly.dev/
-
Send HTTP requests to the app's URL.
$ curl https://fly-http-and-udp.fly.dev/ Hello from HTTP server!
-
Send UDP requests to the app's URL.
$ echo "biraj rocks" | nc -u -w 1 fly-http-and-udp.fly.dev 8080 Received UDP: biraj rocks
In the same directory as this app, run:
$ fly logs
Or you can view the logs on Grafana by going to the app's dashboard and clicking on Log search on the left.
Make sure that a machine is running on fly.io.
$ fly ssh
fly-global-services
is a hostname in/etc/hosts
file on the fly.io machine. You can check it byssh
ing into the machine.- A dedicated ipv4 costs $2 per month.