Introduction
LibXMTP provides a comprehensive messaging system built on MLS (Messaging Layer Security) that supports various content types, message enrichment, and real-time streaming. Messages are the core unit of communication in XMTP groups and conversations.Core message types
LibXMTP distinguishes between several message representations:StoredGroupMessage
Raw message data as stored in the local database. Contains encrypted message bytes, metadata, and delivery status. Key fields:id- Unique message identifier (bytes)group_id- Group this message belongs to (bytes)decrypted_message_bytes- Encrypted content payloadsent_at_ns- Timestamp in nanosecondssender_inbox_id- Inbox ID of sendersender_installation_id- Installation ID of senderkind- Message kind (application, membership change, etc.)delivery_status- Published, unpublished, or failedcontent_type- Type of content (text, reaction, etc.)
DecodedMessage
Enriched message with decoded content, reactions, and reply metadata. This is the primary type used when displaying messages to users. Structure:Message (Node.js bindings)
Simplified message representation exposed to Node.js applications:Message lifecycle
Sending messages
- Encode content - Convert your data to
EncodedContentusing a codec - Send - Call
conversation.send()with encoded content and options - Publish - Message is encrypted, signed, and sent to the network
- Store - Message is saved to local database
shouldPush- Whether to trigger push notificationsoptimistic- If true, returns immediately without waiting for network confirmation
Receiving messages
Messages can be retrieved in two ways: Query historical messages:Message enrichment
Thelist_enriched_messages() / listEnrichedMessages() methods return DecodedMessage instances with additional context:
- Reactions - All reactions to this message
- Reply count - Number of replies referencing this message
- Reply context - For reply messages, includes the referenced message
- Deletion state - Marks messages deleted by sender or admin
Message filtering
Filter messages by various criteria:sentAfterNs/sentBeforeNs- Time range in nanosecondslimit- Maximum number of messagesdirection-'ascending'or'descending'contentTypes- Filter by content typedeliveryStatus- Filter by delivery status
Optimistic sending
For faster UI updates, use optimistic sending:Message deletion
Messages can be deleted by the sender or by group admins:Message kinds
Messages have different purposes indicated by theirkind:
- Text, reactions, replies
- Attachments, read receipts
- Custom content types
- Members added/removed
- Admin role changes
- Group metadata updates
Delivery status
Track message delivery state:Best practices
Performance
- Use
listMessages()for basic message lists (faster) - Use
listEnrichedMessages()only when you need reactions/replies - Implement pagination with
limitand time-based filtering - Stream messages for real-time updates instead of polling
Error handling
Content type detection
Always check content type before accessing type-specific fields:See also
- Content Types - Supported message content types
- DecodedMessage - Enriched message structure
- Sending Messages - Detailed guide on message handling
