MessageEvent
ClassEvent 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
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
Properties
Methods
Example
const event = script.createEvent("MessageEvent");
event.bind((e: MessageEvent) => {
if (e.topic !== "my-tool") return;
e.reply({ status: "ok" });
});
Extends
Properties
data
readonlydata: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
replyToken
readonlyreplyToken: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
readonlytopic: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
readonlytype: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
| Parameter | Type |
|---|---|
evCallback | (arg1) => void |
Returns
void
Inherited from
getTypeName()
staticgetTypeName():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
getTypeName()
getTypeName():
string
Returns the typename of the SceneEvent.
Returns
string
Inherited from
isOfType()
isOfType(
type):boolean
Returns true if the object matches or derives from the passed in type.
Parameters
| Parameter | Type |
|---|---|
type | string |
Returns
boolean
Inherited from
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
| Parameter | Type |
|---|---|
other | ScriptObject |
Returns
boolean
Inherited from
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
| Parameter | Type |
|---|---|
data | any |
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);
});