CLEMA protocol

Calls

Two people of a direct conversation can talk by voice. Calls are signalled over the connection like everything else; the voice itself goes through the service's media relay as Call Media describes. The server gives each call its keys and forgets them when it ends; it keeps no recording of a call, and history keeps only its record.

A call rings the callee's clients that handle calls: connections, passive or not, whose connection.hello declared call.incoming, and devices registered for pushes whose client declared it when it connected last. A call involves one client of each party: the caller's that created it and the callee's that answered. Either party's client is its persistent session, so a connection that drops and comes back for the same session is still in the call.

Make a Call

{
  "id": "70",
  "type": "call.create",
  "data": {"conversation_id": "direct:12345:67890", "caps": 3}
}

caps says what the client can decode, as Call Media defines it. The caller must be at their client: a passive connection cannot call, and gets invalid_state. A call goes between the two participants of a direct conversation; another kind of conversation is invalid_state. The callee's privacy decides who may call: a caller they did not let gets not_authorized with reason contacts_only, where only the callee's contacts may call, or calls_off, where nobody may. A block, and a callee who may not be written to, are not_authorized with no reason, as for messages. A callee with no client that handles calls is invalid_state with reason unavailable. An account barred from calls is restricted.

The response contains call_id, msg_id (the call's record), conversation_id, from, to and expires_in, the seconds it rings for. A callee already in a call, on any client, is not rung: the call ends at once with call.ended and reason busy. Otherwise every ringing client of the callee receives, after the record's message.created:

{
  "type": "call.incoming",
  "data": {
    "call_id": "7c1f5be0a8e44d0f9b0e3f9a1d2c4b6e",
    "msg_id": "12430",
    "conversation_id": "direct:12345:67890",
    "from": 67890,
    "to": 12345,
    "caps": 3,
    "expires_in": 45
  }
}

A client that connects while a call to its user rings receives its call.incoming too, with what is left of expires_in.

Glare. When A calls B while B's call to A still rings, the earlier call wins: A is answered invalid_state with reason glare, and A's clients receive B's call.incoming.

Answer, Decline or Hang Up

A ringing client of the callee answers with:

{"id":"71","type":"call.accept","data":{"call_id":"7c1f5be0a8e44d0f9b0e3f9a1d2c4b6e","caps":3}}

The response contains media, what the client needs to carry the call's voice. The caller's client receives call.accepted with call_id, user_id, the callee's caps and its own media; the callee's other clients receive call.ended with reason answered_elsewhere. A passive connection may answer: the person answered a notification. The first client to answer takes the call; any later call.accept is invalid_state.

{
  "media": {
    "udp": "relay.example.com:3478",
    "ws": "wss://server.example.com/calls/media",
    "session_id": 16909060,
    "token": "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=",
    "send_key": "aVSAwm0pHSgPBz+dPQ94ofaNWOPkMpTU3aFDbnKprTU=",
    "receive_key": "gFEsRcBxxlDdtdI5WxSJuI6L+d63hy1I7m7xxTL+flY="
  }
}
Field Description
udp The relay's UDP address, host:port; absent where the service relays over WebSocket only
ws The relay's WebSocket URL
session_id The call's relay session, put in every media packet
token This client's relay token, base64: it authenticates the client's registrations
send_key AES-256 key, base64, for the packets this client sends
receive_key AES-256 key, base64, for the packets it receives

The two clients are given each other's keys the other way round, and each its own token. Keys are random for every call, travel only in these two messages and are not kept once the call ends.

The callee may instead decline, reason busy where it is in another call the service does not know of, such as a phone call:

{"id":"72","type":"call.decline","data":{"call_id":"7c1f5be0a8e44d0f9b0e3f9a1d2c4b6e","reason":"busy"}}

Either party hangs up with call.hangup and call_id, the caller also while it rings. When both clients are heard by the relay, both receive call.connected with call_id. A call ends for both with call.ended:

{"type":"call.ended","data":{"call_id":"7c1f5be0a8e44d0f9b0e3f9a1d2c4b6e","reason":"hangup","user_id":12345,"duration_ms":192000}}
Reason Meaning
hangup A party hung up a connected call; user_id says who
cancelled The caller hung up while it rang
declined The callee declined
busy The callee was in a call already, or declined as busy
no_answer It rang for expires_in and nobody answered
answered_elsewhere To the callee's other clients: one of them answered
unavailable A block ended it
failed Answered, but the voice did not get through within 10 seconds, or the relay lost the call
timeout Connected, and the relay heard nothing from a party for 20 seconds

duration_ms is the time since call.connected, present once a call connected. A connected call does not end when a party's connection drops: it lasts as long as the relay carries its voice. It does not end when the server restarts either: the relay keeps it, and the server finds it again.

Call Record

A call is also written into the direct conversation as a message of kind call, from the caller, with the message.created that comes before call.incoming. Nobody sends one — message.send refuses the kind — and it cannot be edited or forwarded; it can be replied to, reacted to, pinned and deleted like any message. For the callee it is unread like any message; a callee who answers has read it, when it is all they have unread in the conversation. It wakes nobody: the call's own push does.

{
  "msg_id": "12430",
  "conversation_id": "direct:12345:67890",
  "from": 67890,
  "kind": "call",
  "payload": {
    "call_id": "7c1f5be0a8e44d0f9b0e3f9a1d2c4b6e",
    "state": "ended",
    "reason": "hangup",
    "duration_ms": 192000
  },
  "time": "2024-01-15T14:50:00Z",
  "revision": 3,
  "reactions": []
}

Each move of state is a message.edited with the next revision and no edited_at, and a record only moves up the table, from ringing to active to one of the states that end it:

State Meaning
ringing Ringing the callee
active Connected; the parties are talking
ended Was connected; reason and duration_ms say how it ended and how long it lasted
missed Nobody answered: reason is no_answer, cancelled or unavailable
declined The callee declined
busy The callee was in a call already, or declined as busy
failed Answered, but the voice did not get through

A record is deleted, for oneself or for everybody, once the call ended: while it says ringing or active, message.delete answers invalid_state. When the server starts, a record still ringing becomes missed, and one still active that the relay no longer carries ended with reason failed; duration_ms of such a call is what the server last knew of it.

Call History

call.list pages through the user's call records in every direct conversation, newest first:

{"id":"73","type":"call.list","data":{"before":"12430","limit":50,"missed":true}}

before starts after that record, limit is 1 to 100, 50 when absent, and missed: true keeps only calls to the user that were missed. The response contains calls, the records as Message objects, and more, whether older ones remain. Records deleted for the user are left out.