Skip to content

Trusting the Sender ​

A realtime message carries two kinds of data:

  • What the server sets. It comes from the sender's verified login, and the sender cannot change it. Your app can rely on it.
  • What the sender writes. That is the message itself. Treat it as user input, like any form field.

This page lists which is which, so your app can tell a message from an admin apart from one any user could have written.

Who Sent It: sender_type ​

Every "message" and "private" event says who sent it:

sender_typeWhosender
"owner"The project owner, from their Skapi accountThe owner's user ID
"admin"A user in access group 90 to 99The user's ID
"user"Any other user of the projectThe user's ID

The server sets both fields from the sender's login. A user cannot send a message that arrives as "admin" or "owner", and cannot send one that names another user as sender. A message sent with a login from another project is refused.

TIP

The access group is read from the sender's login. A user whose access group changes keeps their old sender_type until their login refreshes, at most a day later. To apply a change at once, have the user sign in again.

The Fields of an Event ​

In the connectRealtime callback, these fields are set by the server:

FieldWhat you can rely on
typeHow the message was delivered: "private" (sent to you), "message" (sent to the group you joined), "notice" (someone joined or left).
senderThe sender's user ID.
sender_type"owner", "admin" or "user". No value on notices and connection events.
sender_cidThe connection the message came from.
sender_ridThe group, for group messages and notices.
codeUSER_JOINED, USER_LEFT or USER_DISCONNECTED, on notices.

message is what the sender wrote. Any label inside it, such as {"kind": "announcement"}, is the sender's word, not the server's.

Example: an announcement only admins can make ​

kotlin
skapi.connectRealtime { rt ->
    val type = rt["type"]?.jsonPrimitive?.contentOrNull
    if (type != "private" && type != "message") return@connectRealtime

    val senderType = rt["sender_type"]?.jsonPrimitive?.contentOrNull
    val fromStaff = senderType == "admin" || senderType == "owner"
    val body = rt["message"] as? JsonObject

    if (body?.get("kind")?.jsonPrimitive?.contentOrNull == "announcement") {
        // Anyone can write "announcement" in their message: show it as one only from staff.
        if (fromStaff) showAnnouncement(body["text"]?.jsonPrimitive?.contentOrNull)
        return@connectRealtime
    }

    showChatMessage(
        text = body?.get("text")?.jsonPrimitive?.contentOrNull,
        from = rt["sender"]?.jsonPrimitive?.contentOrNull, // a real user ID, safe to look up with getUsers()
        badge = if (fromStaff) "Staff" else null,
    )
}
java
skapi.connectRealtime(rt -> {
    Object type = rt.get("type");
    if (!"private".equals(type) && !"message".equals(type)) return;

    Object senderType = rt.get("sender_type");
    boolean fromStaff = "admin".equals(senderType) || "owner".equals(senderType);
    Map<?, ?> body = rt.get("message") instanceof Map ? (Map<?, ?>) rt.get("message") : Map.of();

    if ("announcement".equals(body.get("kind"))) {
        // Anyone can write "announcement" in their message: show it as one only from staff.
        if (fromStaff) showAnnouncement((String) body.get("text"));
        return;
    }

    showChatMessage(
        (String) body.get("text"),
        (String) rt.get("sender"), // a real user ID, safe to look up with getUsers()
        fromStaff ? "Staff" : null
    );
}).get();

Example: muting a user ​

sender cannot be faked, so a mute list keyed by user ID works:

kotlin
val muted = setOf("<user id>")

skapi.connectRealtime { rt ->
    val sender = rt["sender"]?.jsonPrimitive?.contentOrNull
    val senderType = rt["sender_type"]?.jsonPrimitive?.contentOrNull
    if (sender in muted && senderType == "user") return@connectRealtime // staff is never muted
    handle(rt)
}
java
Set<String> muted = Set.of("<user id>");

skapi.connectRealtime(rt -> {
    Object sender = rt.get("sender");
    if (sender != null && muted.contains(sender) && "user".equals(rt.get("sender_type"))) return; // staff is never muted
    handle(rt);
}).get();

What Can Be Trusted ​

Trust itWhy
sender, sender_typeYesSet by the server from the sender's login.
type, sender_cid, sender_rid, codeYesSet by the server.
messageNoWritten by the sender.
A role or label inside messageNoWritten by the sender. Use sender_type instead.

Limiting What Users Send ​

The project owner can limit how many realtime messages and notifications each user sends in a minute, or stop users from sending any. Admins and the owner are never limited. See Send Limits.

skapi-kotlin 2.7.0