Skip to main content

MessageEvent

Class

Event fired when a message is received from the host (Lens Studio or an AI agent).

  • Snapchat: not supported
  • SPECS: fully supported
  • Camera Kit Web: not supported
  • Camera Kit Mobile: not supported
  • SnapchatNot supported — Available on SPECS only
  • SPECSFully supported
  • Camera Kit WebNot supported — Available on SPECS only
  • Camera Kit MobileNot supported — Available on SPECS only

Platform details

  • Snapchat — Not supported — Available on SPECS only.

  • SPECS — Fully supported.

  • Camera Kit Web — Not supported — Available on SPECS only.

  • Camera Kit Mobile — Not supported — Available on SPECS only.

Since Lens Scripting v373

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.

Index

Example

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

Extends

Properties

data

readonly data: any

SPECS

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

Since

Lens Scripting Version 373


enabled

enabled: boolean

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

Default Value

true

Inherited from

SceneEvent.enabled


replyToken

readonly replyToken: string

SPECS

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.

Since

Lens Scripting Version 373


topic

readonly topic: string

SPECS

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

Since

Lens Scripting Version 373


type

readonly type: string

SPECS

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

Since

Lens Scripting Version 373

Methods

bind()

bind(evCallback): void

Binds a callback function to this event.

Parameters

ParameterType
evCallback(arg1) => void

Returns

void

Inherited from

SceneEvent.bind


getTypeName()

static getTypeName(): TypeName<MessageEvent>

SPECS

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

TypeName<MessageEvent>

Since

Lens Scripting Version 373

Overrides

SceneEvent.getTypeName


getTypeName()

getTypeName(): string

Returns the typename of the SceneEvent.

Returns

string

Inherited from

SceneEvent.getTypeName


isOfType()

isOfType(type): boolean

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

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

SceneEvent.isOfType


isSame()

isSame(other): boolean

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

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

SceneEvent.isSame


reply()

reply(data, token?): void

SPECS

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

ParameterType
dataany
token?string

Returns

void

Since

Lens Scripting Version 373

Example

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);
});