Overview
TheMessagingClient is the Room API for real-time participant-to-participant communication. Use it to send direct messages, broadcast updates, receive live events, and open streams for larger or incremental payloads.
Why use the Messaging API?
- Build chat, agent-human collaboration, and live status updates inside a room.
- Send typed JSON payloads instead of inventing a custom messaging protocol.
- Broadcast announcements to everyone in a room or send point-to-point messages to one participant.
- Stream larger payloads or partial updates when a single message is not enough.
How it works
Messaging is participant-based. Once enabled, the client tracks remote participants and emits events for messages and participant changes. Use direct messages for one-to-one delivery, broadcasts for room-wide updates, and streams when the payload arrives in chunks or over time.Core concepts
Participant model
Messaging treats every room member as aParticipant, whether that participant is a human, an agent, or another service. That gives you one delivery model for direct messages, broadcasts, and stream setup instead of separate APIs for different participant types.
Typed JSON messages
Each message has atype plus a JSON payload. That makes it easy to build simple chat flows, structured commands, workflow events, or agent-to-agent coordination on top of the same messaging surface.
Optional attachments
Messages can also carry binary attachments. Use this when you need to send raw bytes along with the JSON payload, for example for images, voice snippets, or other generated artifacts.Streams for larger payloads
For large or incremental payloads, use streams instead of a single message. A stream opens between two participants, sendsMessageStreamChunk values over time, and closes when the sender is done.
Event-driven behavior
Once messaging is enabled, the client can emit events for incoming messages, participant changes, and accepted streams. That makes it suitable for real-time workflows where your application reacts immediately when another participant joins, sends a message, or starts a stream.Permissions and grants
If a deployed service needs to send or receive room messages, its participant token needs the appropriate Room API grants. See API Scopes and Packaging and Deploying Services.CLI and SDK availability
- CLI: inspect participants and send or broadcast simple messages with
meshagent room messaging .... - Python, JavaScript/TypeScript, Dart, and .NET: the live messaging client supports events and streams in addition to direct messages.
- Streams and event subscriptions: use the SDKs for these; the CLI is mainly for inspection and simple message delivery.
Getting Started Example
Basic Example: Sending and Receiving Messages
Example: Creating and Handling Streams
API Methods
enable(onStreamAccept?)
Enables the messaging subsystem on the server. Optionally provides a callback that is triggered whenever an incoming stream is opened by a remote participant.disable()
Disables the messaging subsystem on the server.sendMessage(to, type, message, attachment?)
Sends a message to a specific participant, optionally including a binaryattachment.
Parameters:
to– The participant to receive the message.type– A string that categorizes or labels the message.message– A JSON-serializable object containing message data.attachment– (Optional) AUint8Arrayof binary data to attach.
broadcastMessage(type, message, attachment?)
Broadcasts a message to all participants in the room. Parameters:type– A string that categorizes or labels the message.message– A JSON-serializable object containing message data.attachment– (Optional) AUint8Arrayof binary data to attach.
createStream(to, header)
Creates a new stream directed at a single participant. Returns aMessageStreamWriter once the remote participant accepts the stream.
Parameters:
to– The participant to receive the stream.header– A JSON-serializable object describing the stream.
A
Promise<MessageStreamWriter> that resolves once the remote participant accepts the stream.
remoteParticipants
Provides an iterable collection of all known remote participants.MessageStreamChunk
Represents a chunk of data in a stream, consisting of a header and optional binary data.MessageStreamWriter
A writer for sending streaming data to a remote participant, created viaMessagingClient.createStream().
-
write(MessageStreamChunk)
Sends a chunk of data in the ongoing stream. -
close(): Promise<void>
Closes the stream, signaling to the remote participant that no more chunks will be sent.
Additional Notes
- Event emission
MessagingClientextendsEventEmitter<RoomMessageEvent>. It emits events whenever messages are received or streams are opened/closed. You can listen for the"message"event or for custom events like"participant_added","participant_removed", etc.:
-
Remote Participants
MessagingClientautomatically keeps track of participants joining or leaving when the messaging subsystem is enabled. You can access them viaremoteParticipants. -
Error Handling
- If a request fails or the server returns an error, the underlying
RoomClientmay throw an exception. - Stream operations (
createStream,write, etc.) may also fail if the remote participant rejects or if the connection is lost.
- If a request fails or the server returns an error, the underlying
-
Extensibility
You can add additional features—like chunk-level encryption, compression, or custom handshake logic—by extendingMessagingClientor by wrapping the provided stream objects (MessageStreamWriter/MessageStreamReader). -
Performance
For large or frequent streams, consider buffering or batching chunk writes in your client logic. The streaming mechanism inMessagingClientis designed to handle incremental data transfer, but network considerations remain important for efficient performance.