@urth/metatell-sdk - v0.1.0
    Preparing search index...

    User Profile

    @urth/metatell-sdk/user exposes the current user's own profile, including the display name. A typical use is to show the player's name inside a plugin such as a mini-game.

    The relevant APIs are:

    • getSessionId() — read the current room-session id, including as a guest
    • getId() — read the persistent account id
    • getProfile() — read your own profile synchronously
    • onProfileUpdate(handler) — subscribe to changes to your own profile

    These run in the browser-side plugin runtime. The metatell host must provide window.APP at runtime.

    getSessionId() returns the current Phoenix room-session id. It is available to guest users, remains stable for the duration of the session, and can change after leaving and re-entering a room. Use getId() instead when persistent account identity is required.

    import { getSessionId } from "@urth/metatell-sdk/user";

    const sessionId = getSessionId();

    The function returns undefined until the metatell host publishes the current session.

    The session id is the same identifier other users see as RemoteUser.sessionId in /users, ChatMessage.from.sessionId in Chat, and the sessionId of an object's lipSync binding in Room Objects.

    getId() returns the persistent account id (the sub claim of the host's metatell token). It is also available as user.id on the root entry.

    import { getId } from "@urth/metatell-sdk/user";

    const userId = getId();

    It returns undefined when the host has no account token. The token is decoded without signature verification, so never use this value as proof of identity for your backend; use Auth instead.

    getProfile() returns the current user's (self) profile synchronously. No network round trip is performed.

    import { getProfile } from "@urth/metatell-sdk/user";

    const profile = getProfile();
    console.log(profile?.displayName);

    The returned UserProfile has the following shape:

    interface UserProfile {
    displayName: string;
    avatarId?: string;
    roles?: string[];
    }

    getProfile() returns undefined when the metatell host is not ready, or when the display name has not been resolved yet. Always null-check the result, for example with profile?.displayName.

    This API returns only your own profile. To read other users in the room, use @urth/metatell-sdk/users.

    To be notified when the user changes their display name or avatar, use onProfileUpdate(). It returns an unsubscribe function. Always call it during plugin teardown.

    import { onProfileUpdate } from "@urth/metatell-sdk/user";

    const unsubscribe = onProfileUpdate((profile) => {
    nameLabel.textContent = profile.displayName;
    });

    // later, on teardown
    unsubscribe();
    • To control whether the user can move, see User Movement.
    • To call your own backend with a scoped plugin API token, see Auth.