Message
On message
hook, you receive a message object containing data from the client.
MessageEvent
with convenient superset of utils.Instance properties
message.id
Unique random identifier (uuid v4) for the message.
message.event
Access to the original message event if available.
message.peer
Access to the peer instance that emitted the message.
message.rawData
Raw message data (can be of any type).
message.data
Message data (value varies based on peer.binaryType
).
Instance methods
message.text()
Get stringified text version of the message.
If raw data is in any other format, it will be automatically converted or decoded.
message.json()
Get parsed version of the message text with JSON.parse()
.
unjs/destr
to safely parse the message object.It does not throw an error if the input is not valid JSON but falls back to text and also removes any fields that could potentially cause prototype pollution vulnerabilities.const data = destr(await message.text());
message.uint8Array()
Get data as Uint8Array
value.
If raw data is in any other format or string, it will be automatically converted or encoded.
message.arrayBuffer()
Get data as ArrayBuffer
or SharedArrayBuffer
value.
If raw data is in any other format or string, it will be automatically converted or encoded.
message.blob()
Get data as Blob
value.
If raw data is in any other format or string, it will be automatically converted or encoded.
Adapter support
Bun | Cloudflare | Cloudflare (durable) | Deno | Node (ws) | Node (μWebSockets) | SSE | |
---|---|---|---|---|---|---|---|
event | ⨉ | ✓ | ⨉ | ✓ | ⨉ | ⨉ | ⨉ |