CLEMA protocol

Handshake

After establishing a WebSocket connection, the client must send connection.hello to declare supported message types (capabilities). This must be the first message before any other communication.

Hello Request

Client:

{
  "type": "connection.hello",
  "data": {
    "client": {"device_name": "Pixel 8", "platform": "Android", "name": "Mandarin", "version": "2.1", "build": 693, "language": "ru-RU"},
    "types": [
      "auth.code", "code.get", "auth.phone", "auth.qr", "auth.token", "auth.refresh", "auth.logout", "qr.get", "qr.accept",
      "session.list", "session.revoke", "session.update", "account.get", "account.code", "account.delete", "phone.get", "phone.code", "phone.set", "push.register", "push.unregister", "activity.set",
      "transport.list", "transport.setup", "transport.password", "transport.enable", "transport.disable", "transport.reset",
      "message.send", "message.forward", "message.edit", "message.delete", "message.react", "message.play",
      "message.list", "message.search", "message.clear", "read.set", "delivery.set",
      "pin.set", "pin.remove", "pin.list", "notification.set", "posting.set", "draft.update", "reactions.see", "mentions.see",
      "block.add", "block.remove", "block.list", "privacy.get", "privacy.update",
      "ban.add", "ban.remove", "ban.list", "restriction.set", "sync.state", "sync.pull",
      "conversation.create", "conversation.list", "conversation.search", "conversation.get", "conversation.update", "conversation.delete",
      "member.list", "member.add", "member.update", "member.remove",
      "invite.create", "invite.list", "invite.get", "invite.accept", "invite.revoke", "invite.update",
      "contact.list", "contact.add", "contact.rename", "contact.delete", "link.get", "link.reset", "link.open", "link.accept",
      "phonebook.update", "phonebook.get", "phonebook.clear", "phonebook.match",
      "presence.get", "presence.set", "profile.get", "profile.update", "user.search", "typing.set",
      "file.create", "file.get", "file.complete", "file.abort",
      "transfer.create", "transfer.accept", "transfer.decline", "transfer.cancel",
      "call.create", "call.accept", "call.decline", "call.hangup", "call.list", "button.press", "button.answer",
      "pack.list", "pack.get", "pack.install", "pack.remove", "pack.order", "report.create",
      "session.revoked", "code.updated", "account.updated", "transport.updated", "phone.updated", "qr.accepted",
      "message.created", "message.edited", "message.deleted", "message.reacted",
      "message.cleared", "message.played", "read.updated", "read.reached", "delivery.updated", "pin.added", "pin.removed", "notification.updated", "posting.updated", "draft.updated",
      "reactions.updated", "mentions.updated",
      "block.added", "block.removed", "privacy.updated",
      "ban.added", "ban.removed", "restriction.updated",
      "conversation.created", "conversation.updated", "conversation.deleted",
      "member.added", "member.updated", "member.removed",
      "invite.created", "invite.accepted", "invite.revoked", "invite.updated",
      "contact.added", "contact.renamed", "contact.deleted", "contact.requested",
      "presence.online", "presence.offline", "presence.updated", "presence.self", "profile.updated", "file.updated", "typing.changed",
      "transfer.created", "transfer.accepted", "transfer.declined", "transfer.cancelled",
      "call.incoming", "call.accepted", "call.connected", "call.ended", "button.pressed",
      "pack.installed", "pack.removed", "pack.ordered",
      "announcement.updated", "announcement.removed"
    ]
  }
}
Field Type Description
types array of strings Message types the client supports
active boolean Optional, default true. false opens the connection passive, as a background sync does
client object What the client is: the session the connection signs in on is labelled by it, and the service may no longer support its build

client is untrusted metadata, said once for the connection, signed in or not. Each field is trimmed and must contain valid text without control characters. device_name is limited to 80 characters; platform, name, and version to 40 characters each. platform is one of Android, iOS, macOS, Windows and Web — a web client is Web whatever system its browser runs on — as written here: the service names a client's builds and whom an announcement is for by it. build is the number of the client's build, an integer greater for each build after, such as Android's versionCode or the build's number in continuous integration — never its version string: the service supports a client by it, and 0, or none, is a client that does not say, which counts as below any lowest build set for it. A platform named otherwise is no client's whose builds or announcements the service sets. language, a BCP 47 tag, is what its user reads: Mandarin writes to the account in the language of the session used last that gave one. console: true says the client is Mandarin's staff console, not a messenger: signed in, its connection may only renew its tokens (auth.refresh) and sign out (auth.logout), is sent no events and makes nobody online; anything else it asks answers not_authorized. A messenger leaves it out. A field malformed so is invalid_format. A connection says its hello once: another is invalid_state.

Hello Response

Success:

{
  "type": "connection.hello",
  "ok": true,
  "data": {
    "types": [
      "auth.code", "code.get", "auth.phone", "auth.qr", "auth.token", "auth.refresh", "auth.logout", "qr.get", "qr.accept",
      "session.list", "session.revoke", "session.update", "account.get", "account.code", "account.delete", "phone.get", "phone.code", "phone.set", "push.register", "push.unregister", "activity.set",
      "transport.list", "transport.setup", "transport.password", "transport.enable", "transport.disable", "transport.reset",
      "message.send", "message.forward", "message.edit", "message.delete", "message.react", "message.play",
      "message.list", "message.search", "message.clear", "read.set", "delivery.set",
      "pin.set", "pin.remove", "pin.list", "notification.set", "posting.set", "draft.update", "reactions.see", "mentions.see",
      "block.add", "block.remove", "block.list", "privacy.get", "privacy.update",
      "ban.add", "ban.remove", "ban.list", "restriction.set", "sync.state", "sync.pull",
      "conversation.create", "conversation.list", "conversation.search", "conversation.get", "conversation.update", "conversation.delete",
      "member.list", "member.add", "member.update", "member.remove",
      "invite.create", "invite.list", "invite.get", "invite.accept", "invite.revoke", "invite.update",
      "contact.list", "contact.add", "contact.rename", "contact.delete", "link.get", "link.reset", "link.open", "link.accept",
      "phonebook.update", "phonebook.get", "phonebook.clear", "phonebook.match",
      "presence.get", "presence.set", "profile.get", "profile.update", "user.search", "typing.set",
      "file.create", "file.get", "file.complete", "file.abort",
      "transfer.create", "transfer.accept", "transfer.decline", "transfer.cancel",
      "call.create", "call.accept", "call.decline", "call.hangup", "call.list", "button.press", "button.answer",
      "pack.list", "pack.get", "pack.install", "pack.remove", "pack.order", "report.create",
      "session.revoked", "code.updated", "account.updated", "transport.updated", "phone.updated", "qr.accepted",
      "message.created", "message.edited", "message.deleted", "message.reacted",
      "message.cleared", "message.played", "read.updated", "read.reached", "delivery.updated", "pin.added", "pin.removed", "notification.updated", "posting.updated", "draft.updated",
      "reactions.updated", "mentions.updated",
      "block.added", "block.removed", "privacy.updated",
      "ban.added", "ban.removed", "restriction.updated",
      "conversation.created", "conversation.updated", "conversation.deleted",
      "member.added", "member.updated", "member.removed",
      "invite.created", "invite.accepted", "invite.revoked", "invite.updated",
      "contact.added", "contact.renamed", "contact.deleted", "contact.requested",
      "presence.online", "presence.offline", "presence.updated", "presence.self", "profile.updated", "file.updated", "typing.changed",
      "transfer.created", "transfer.accepted", "transfer.declined", "transfer.cancelled",
      "call.incoming", "call.accepted", "call.connected", "call.ended", "button.pressed",
      "pack.installed", "pack.removed", "pack.ordered",
      "announcement.updated", "announcement.removed"
    ]
  }
}

Server responds with the list of types it supports. The intersection of client and server types determines what can be used in the session. A signed-in connection's session remembers the intersection its client said last, for when nothing of it is connected: a device a push wakes is woken only for what its client can answer, such as a direct transfer's offer.

A server whose service has links of its own also answers link_base, such as "link_base":"https://mandarin.im": where every link it hands out starts — invitations, bots, message links and QR sign-in codes. A client opens such links itself instead of leaving them to a browser, and an app of the service claims them on its platform (Android App Links, iOS Universal Links). Without link_base there are no such links.

The answer also carries announcements: what the service announces to the client's platform now, none when it announces nothing.

How large a file a client may send is the account's to know, once signed in: see Account Standing.

Failure:

{
  "type": "connection.hello",
  "ok": false,
  "error": {
    "code": "no_common_types",
    "message": "No common message types supported"
  }
}

A build of a client the service no longer supports is answered client_outdated, saying where to get one it does, and its connection is closed:

{"type":"connection.hello","ok":false,"error":{"code":"client_outdated","message":"This build of the client is no longer supported","update":{"build":700,"url":"https://play.google.com/store/apps/details?id=com.tomclaw.mandarin"}}}

Supported Builds

The service supports the builds of each client, by its name and platform, from the lowest its staff set on. A client's builds below it are refused: its hello, or, for a client whose hello did not say what it is, its sign-in by token, judged by what its session is labelled, is answered client_outdated, and a connection open as the lowest build is raised is told so as it closes (see Disconnect). So is a client of any build that speaks a protocol the server no longer serves. update says the lowest build supported, build, and where to get it, url, where the service's staff set them; a client told without them sends its users where it is distributed. A client so refused tells its user to update, and connects no further until it is updated. A client that does not say its name is never refused.

Announcements

The service announces what its clients show their users as its own state, rather than as a message: a maintenance window, a fault it has now, or news. Every connection is told what is announced to its platform as its hello is answered, signed in or not, and those that declared announcement.updated and announcement.removed are told as an announcement is made, changed or withdrawn:

{"type":"announcement.updated","data":{"announcement_id":7,"kind":"maintenance","text":"Mandarin moves to a new server and will be unavailable for up to 30 minutes.","languages":{"ru":{"text":"Mandarin переезжает на новый сервер и будет недоступен до 30 минут."}},"starts_at":"2026-10-28T02:00:00Z","ends_at":"2026-10-28T02:30:00Z","updated_at":"2026-10-27T12:00:00Z"}}
{"type":"announcement.removed","data":{"announcement_id":7}}
Field Type Description
announcement_id number The announcement, the same through its changes
kind string maintenance, a window the service is down in; incident, a fault it has now; info, news
text string What it says
languages object What it says to the readers of other languages, by primary subtag (ru): text in place of the one above
starts_at, ends_at string A maintenance window's start and end. Any other kind starts as it is announced, and may end
updated_at string When it was made or last changed

An announcement ends at ends_at, when the client drops it without being told; one withdrawn before is announcement.removed, as is one changed so as to be for the client's platform no longer. A client keeps what it was told, to show while it cannot connect: inside a maintenance window it says the service is under maintenance until the window ends, rather than that it is connecting. It shows the most pressing at a time: a window under way, then a fault, then a window ahead, then news, which its user may put away.

Capability Negotiation

Protocol Evolution

When a message type needs breaking changes, a new type is introduced:

// Existing client
{"types": ["message.send", ...]}

// New client with updated message format
{"types": ["message.send", "message.send.v2", ...]}

Server uses the most advanced type both parties support. Old clients continue to work with original types.

An event of a stream is kept once, in no particular form, and given its form as it is delivered — live or in a pull: the newest version of its type the client declared, the original one when it declared none. A stream therefore holds one event for every version, and a client that knows only the original form reads every place of it.

A type gives way to the one in its place for good once next to no client uses it: the server retires it, and serves it no more. A client whose hello offers a retired type and not the one in its place speaks the protocol of before, whatever its build: it is answered client_outdated, as a build no longer supported is, and connects no further until it is updated. A client offering both is served the newer. So the protocol grows without turning old clients away, until a type is retired; a change no type can carry — of the envelope or the handshake — is a new endpoint instead.