Preparing search index...

    Class MessageEvent

    Event fired when a message is received from the host (Lens Studio or an AI agent). Subscribe via ScriptComponent.createEvent with "MessageEvent". Use MessageEvent.topic to route messages and MessageEvent.reply to send a response back to the sender.

    MessageEvent.topic, MessageEvent.type, and MessageEvent.replyToken are only valid inside the bound callback for the message currently being delivered. MessageEvent.reply can be called directly inside the callback, or after asynchronous work by passing a token captured from MessageEvent.replyToken inside the callback.

    Lens Scripting Version 373

    const event = script.createEvent("MessageEvent");
    event.bind((e: MessageEvent) => {
    if (e.topic !== "my-tool") return;
    e.reply({ status: "ok" });
    });

    Hierarchy (View Summary)

    Index

    Properties

    data: any

    The message payload sent by the host. Matches https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/data.

    Lens Scripting Version 373

    enabled: boolean

    If true, the event is able to trigger. If false, the event will not trigger.

    true
    
    replyToken: string

    Token identifying the message currently being delivered, for replying asynchronously. Read it inside the bound callback (before any await) and pass it to MessageEvent.reply once the result is ready.

    Reading the token marks the delivery as deferred, so the sender keeps waiting for the reply. If a callback neither replies nor reads the token, the request is completed with an error as soon as the callback returns, letting the sender fail fast instead of waiting for its timeout. Empty for messages that don't expect a reply.

    Lens Scripting Version 373

    topic: string

    The routing topic of this message event. Use this to filter messages by channel in your bind callback.

    Lens Scripting Version 373

    type: string

    The MIME type of MessageEvent.data, e.g. "application/json".

    Lens Scripting Version 373

    Methods

    • Binds a callback function to this event.

      Parameters

      • evCallback: (arg1: this) => void

      Returns void

    • Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().

      Returns string

      Lens Scripting Version 373

    • Returns the typename of the SceneEvent.

      Returns string

    • Returns true if the object matches or derives from the passed in type.

      Parameters

      • type: string

      Returns boolean

    • Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

      Parameters

      Returns boolean

    • Send a response back to the host that sent this message. Each message may be replied to at most once.

      Called without token, this replies to the message currently being delivered and is only valid inside the bound callback. To reply after asynchronous work (e.g. an await, a timeout, or a later frame), capture MessageEvent.replyToken inside the callback and pass it here.

      data may be any JS value (object, array, string, number, etc.). It is serialized via the stringified-js transport so the host receives the value with its original shape and types (including types that plain JSON cannot represent, e.g. Map/Set/Date/BigInt).

      Parameters

      • data: any
      • Optionaltoken: string

      Returns void

      Lens Scripting Version 373

      const event = script.createEvent("MessageEvent");
      event.bind(async (e: MessageEvent) => {
      const token = e.replyToken;
      const value = await computeAsync(e.data); // any asynchronous work
      e.reply({ result: value }, token);
      });