API
The REST API speaks JSON. Realtime is one WebSocket that speaks MessagePack. Both are served by koko-server. Native clients, the web client and your own tools use the same calls.
Authentication
POST /api/register and POST /api/login take {"username": "...", "password": "..."} and return {"token": "...", "username": "..."}. The username comes back lower case. Send the token on every other call as Authorization: Bearer <token>. POST /api/logout revokes it.
Errors are {"error": "message"} with a status code: 400 invalid input, 401 not signed in or wrong credentials, 403 not allowed, 404 not found, 409 conflict (for example a username that is taken), 429 slow down.
REST
| Call | Body | Reply |
|---|---|---|
GET /healthz |
ok |
|
GET /api/state |
Your username, your servers with channels and members, your DMs | |
POST /api/servers |
{"name"} |
{"server_id"} |
POST /api/servers/{id}/channels |
{"name"} |
{"channel_id"} |
POST /api/servers/{id}/invites |
{"code"} |
|
POST /api/join |
{"code"} |
{"server_id"} |
POST /api/servers/{id}/leave |
{} |
|
POST /api/servers/{id}/members/{name}/role |
{"role": "admin" or "member"} |
{} |
DELETE /api/servers/{id}/members/{name} |
{} kicks a member |
|
POST /api/dms |
{"username"} |
{"channel_id"} |
POST /api/push |
{"kind": "unifiedpush" or "webpush", "endpoint", "p256dh", "auth"} |
{} stores a push endpoint (p256dh and auth only for webpush). Up to 10 per user |
POST /api/push/remove |
{"endpoint"} |
{} |
GET /api/push/key |
{"key"} the server's VAPID public key, for PushManager.subscribe. 404 when the server was built without push |
|
GET /api/channels/{id}/messages?before={id}&limit={n} |
Up to 100 messages, oldest first |
Who may do what: owners and admins create channels. Any member can create an invite. Only the owner changes roles. An owner can remove admins and members, an admin can remove members. The server decides and answers 403 otherwise. Roles appear as "Owner", "Admin" and "Member". In REST replies a message is a JSON object with the field names below.
Push notifications
Push is compiled in with cargo build --release -p koko-server --features push. A send fires only for channel members who have no live socket; online users get the message over the WebSocket and nothing else. Both kinds carry the smallest possible payload, {"channel": <id>, "message": <id>}: never message text, never a sender name.
unifiedpush: a plain POST of that JSON to the endpoint. The distributor (ntfy, a Nextcloud server, and so on) sees the two ids.webpush: the same JSON, encrypted to the subscription keys with RFC 8291aes128gcm, sent with an RFC 8292 VAPID JWT. The push service cannot read it. The VAPID keypair is generated on first use and stored in the database; the public half is atGET /api/push/key. SetKOKO_VAPID_SUBJECTto amailto:the push service can reach you at.
An endpoint that answers 404 or 410 is deleted. Pushes to one user are capped at a short burst per minute.
WebSocket
Connect to /ws. Every frame is a binary MessagePack message. The first frame you send must be Hello, within 10 seconds, or the server closes the connection.
Encoding
The server uses Rust's serde with MessagePack in its compact form:
- A unit variant is its name as a string:
"Ping","Pong". - A variant with fields is a map with one key, the variant name, and an array of the fields in the order listed below:
{"Hello": ["token", 5]}. - A struct (a message) is an array of its fields in the order listed below.
- An absent optional value is nil.
The test golden_frames in koko-protocol pins exact bytes for six frames, for example Ping is a4 50 69 6e 67.
Client to server
| Frame | Fields |
|---|---|
Hello |
token, last_seen (highest message id you have, or 0) |
Send |
channel_id, client_id, content, reply_to |
Edit |
id, content |
Delete |
id |
Typing |
channel_id |
Read |
channel_id, message_id |
Ping |
client_id is a string you choose and keep for the life of the message. Sending the same client_id again returns the same message and does not post a second one. Use a fresh random id per message and reuse it when you retry.
Server to client
| Frame | Fields |
|---|---|
Welcome |
username, gap. When gap is true you missed more than the resume limit: refetch history instead of expecting the missed messages. |
Ack |
message. Your Send was stored. message.id is the server id. |
New |
message. Delivered to every member of the channel, and to your other connections. |
Edited |
message |
Deleted |
id, channel_id |
Typing |
channel_id, username |
Rejected |
client_id, code, message. Drop that message from your outbox, do not retry. |
ReadUpTo |
channel_id, message_id. You read this channel on another device. |
StateChanged |
Refetch /api/state. |
Pong |
|
Error |
code, message |
Message
On the WebSocket an array of: id, channel_id, author, client_id, content, reply_to, created, edited, deleted. Times are Unix milliseconds. A deleted message has deleted true.
Resume
After a reconnect, send Hello with the highest message id you hold. The server sends every message you missed, in order, as New frames, right after Welcome. Deduplicate by id, because a message can arrive both live and in the resume.
Keep alive
The server pings every 25 seconds and closes a connection that has been silent for 70 seconds. Browsers answer protocol pings on their own. Send Ping yourself to check that the path is alive.