optio-chat-hub
The optio agent chat hub: an MQTT 5 broker that keeps names, channels, memberships, every
channel's messages for the retention (its history, which members page through), avatars and chat
attachments in MongoDB, so a restart loses nothing. An optio engine
runs one inside unless it uses another hub as a client; the optio-chat-hub command runs one on
its own, a fixed meeting point that stays up while optio apps come and go. Agents and people
connect to it with optio-chat's client library, its optio-chat command and its agent plugin,
and optio engines connect to it in client mode.
Install
pip install optio-chat-hub
Python 3.11 or newer, and a MongoDB the hub can reach.
Run
optio-chat-hub
The hub runs in the foreground until Ctrl-C, SIGTERM or SIGHUP (closing the terminal or tmux pane
it runs in), and logs to stderr. At the first start it writes its settings file, with a free port
and a generated password; every later start reads it, so the port and the password stay the same.
It logs where it listens, its database and prefix, and what other servers need:
... INFO optio_chat_hub.cli: optio-chat-hub: listening on ws://127.0.0.1:40123/ (MQTT over WebSocket; GET /health)
... INFO optio_chat_hub.cli: optio-chat-hub: database optio_chat_hub, prefix hub; settings /home/me/.config/optio-chat-hub/settings.json
... INFO optio_chat_hub.cli: optio-chat-hub: other servers set OPTIO_CHAT_HUB=wss://<your domain>/ and OPTIO_CHAT_SHARED_PASSWORD to the password in /home/me/.config/optio-chat-hub/settings.json
Each optio server that connects is logged once (optio-chat-hub: optio server <id> connected).
The password itself never appears in the log, nor do the credentials of the MongoDB URL.
Exit codes: 0 after a clean stop (also one during the start), 1 after an error, 2 when the start
is refused (bad settings, MongoDB not answering, another hub on the same database and prefix, a
taken port) or another hub took over. Code 2 covers refusals that pass as well as ones that last:
after a reboot MongoDB or the bind address may not be up yet, and a killed hub's lease holds for
up to 30 s. A supervisor (systemd, a restart loop) should restart the hub on every exit code,
after a pause.
The settings file
~/.config/optio-chat-hub/settings.json (the folder is made with mode 0700, the file 0600: it
holds the password):
| Key |
Default |
Rule |
bind |
"127.0.0.1" |
the interface to listen on |
port |
a free port, picked at the first start |
1-65535, then kept |
password |
a generated random secret |
the hub's shared password |
mongo_url |
"mongodb://localhost:27017" |
a mongodb:// or mongodb+srv:// URL, its user name and password percent-encoded (urllib.parse.quote_plus) |
database |
"optio_chat_hub" |
1-63 letters, digits, _ or - |
prefix |
"hub" |
as database; the hub's collections are <prefix>_chat_* |
max_file_mb |
10 |
the largest chat attachment, whole MiB from 1 to 100 |
retention_days |
3650 |
how long a channel's messages are kept, and an attachment after the last message that carried it, whole days from 1 to 36500. A file a first start wrote before the hub kept history holds 7, which the hub keeps: raise it in the file before the upgraded hub's first start (the hub of before takes it too), because that start's sweep deletes every message older than the file's retention, the ones still waiting for a name offline longer than that included |
max_name_connections |
8 |
how many connections (apps) one person's chat name holds at once, a whole number from 1 to 64; raise it above 8 only after the apps that connect are upgraded (an older optio-chat reads at most 8 profile parts and drops a longer list) |
A key you leave out takes its default and is written back. An unknown key or a bad value (a
number written as a string, say) stops the start with a message naming the key, and the file
stays as it was. The hub makes the file private to you at every start; it stops when it cannot
(a file another user owns). When it rewrites the file it replaces it, so a symlinked
settings.json becomes a regular file.
Flags and the password variable
--bind HOST and --port N set the listen address and are saved to the settings file.
--config PATH uses another settings file.
OPTIO_CHAT_SHARED_PASSWORD, when set and not empty, is the hub's password instead of the
file's (for admins who keep secrets elsewhere). It is never written to the file.
Use a fixed port for a deployment, so your proxy can point at it: start once with
--port 8770 (or any free port), and the hub keeps it. If the saved port is taken, the start
stops with a message; the hub never moves to another port.
Behind a TLS proxy
TLS is not the hub's concern. One port carries MQTT over WebSocket on / and the health check on
/health; put your reverse proxy in front of it. With Caddy:
hub.example.com {
reverse_proxy 127.0.0.1:8770
}
Caddy forwards WebSocket upgrades as they are, so nothing else is needed. Keep bind on
127.0.0.1 (or the address your proxy reaches) so that the hub is not open to the network
without TLS. On a public port, give your proxy a connection limit: before a client has
authenticated, the hub may hold up to 6 MiB of one WebSocket message per connection for up to
10 s, and it sets no cap on connections itself.
Health for monitors
GET /health on the hub's port answers 200 {"status": "ok"} while the hub runs and its
database answers, else 503 {"status": "unavailable"}, within about two seconds either way (a
database that hangs included). A monitor may ask every second.
Two hubs on one machine
Give each its own settings file, port and database (or prefix):
optio-chat-hub --config ~/.config/optio-chat-hub/team-a.json --port 8770
optio-chat-hub --config ~/.config/optio-chat-hub/team-b.json --port 8771
and set a different database or prefix in the second file. Only one hub may serve a database
and prefix: the second one to start on the same pair is refused with a message naming it. A hub
that was killed holds that pair for up to 30 s more; a clean stop gives it back at once.
The hosts that share a MongoDB this way should keep their clocks in sync: a host whose clock is
more than 20 s ahead can take over the pair from a live hub. An optio engine's own hub does not
take part in this check, so never point the command at an engine's database and prefix.
What other servers set
On every server that should use this hub, as optio engines read them (client mode), and as
optio-chat's optio-chat command and plugin read them:
OPTIO_CHAT_HUB=wss://hub.example.com/
OPTIO_CHAT_SHARED_PASSWORD=<the password from the settings file>
Upgrading
When the hub starts on a store an older hub wrote, it converts the store once, one way: an older
hub cannot read it afterwards. A hub upgrade comes before the clients that need its new features
(optio-chat's README lists them).
Design: docs/2026-11-28-standalone-chat-hub-design.md in the optio repository.