Your own chat server on the open XMPP protocol, once known as Jabber. Messages stay on your server, and it talks to other XMPP servers on its own.
Prosody or ejabberd
Prosody is light and takes an afternoon to set up on a Raspberry Pi. ejabberd suits a larger number of users. Both handle OMEMO encryption, file sharing and push notifications.
sudo apt install prosody
sudo prosodyctl adduser you@your-domain.comClients
Conversations on Android, Monal on iOS and macOS, Gajim and Dino on the desktop. You need a domain and a few DNS records; the public IP address comes with the hosting.