Use websockets in Godot on itch.io with Caddy

26. August 2024

To use websockets within your Godot game on itch.io, or any other website, a secure websocket connection, aka wss://, is mandatory.

WARNING: This is probably not the correct way to secure Godot's websocket connections. I'm using this setup only for some real-time auction tool I created for personal use only. Please inform yourself, before using this in production.
If you know the correct way how to solve this (or this actually is the correct way), please le me know!

Caddy for the rescue

The caddy webserver, by default, automatically obtains and renews TLS certificates for all your sites and applications. This includes your websocket server!

With this simple Caddyfile, your websocket server is ready. Change the 8000 port to whatever port you are using. Also the /ws/* can be changed to /whatever/* you prefer.

your.dom.ain {
        reverse_proxy /ws/* localhost:8000
        handle /ws/* {
                reverse_proxy localhost:8000
        }
}

In your Godot client code, you can simply put the secure websocket url.
Just make sure you use wss:// instead of ws://.

const HOST: String = "wss://your.dom.ain/ws/"

Now you can play your game on itch.io or other websites!

Client/Server code

The godot-demo-projects repository has great code examples for networking.

My application uses the code from there and can be found here

This is an application for managing an auction for the Italian fantasy football game Fantacalcio.
In this code, the server is just forwarding the messages from one client to all other connected clients. The server does not store any data or state. If needed, you can extend the server with more logic, handling sessions, store data etc...

Every feedback is welcome

Feel free to write me an email at info@simondalvai.org and comment on Mastodon or HackerNews.

Github button Codeberg button mastodon button RSS button Email button