beginner
+100 XP

On-Chain Messaging on Sui

Learn why decentralized messaging matters, see how the @mysten/messaging SDK works in code, and build your first message flow.

Lesson Syllabus

Why On-Chain Messaging
🔒

The Problem with Centralized Messaging

Traditional messaging platforms like WhatsApp and Discord rely on centralized servers. A single company controls your data and can censor or ban you. If they shut down, your message history vanishes. On-chain messaging eliminates this by storing messages as blockchain objects that you own. Let's see what this looks like architecturally.

🏗️

The Sui Messaging Architecture

Sui messaging uses three layers working together. The Sui blockchain stores channel metadata and message references as objects. The Seal protocol encrypts messages so only channel members can read them. Walrus (Sui's decentralized storage) holds the actual encrypted message blobs. Only a tiny blob ID is stored on-chain, keeping gas costs low. Here's how a message flows through all three layers:

🔗

How the Layers Connect

Each layer has a specific role: Sui handles ownership and access control, Seal handles privacy, and Walrus handles storage. The @mysten/messaging SDK ties them all together through a single MessagingClient. When you call sendMessage(), the SDK automatically encrypts with Seal, stores on Walrus, and records the reference on Sui. Match each component to its role:

Channels and Messages
📡

Channels: Your Messaging Rooms

A channel is a Sui object that represents a conversation. It stores member addresses, an encryption policy, and metadata. Channels can be direct messages (2 members) or group chats (many members). The channel object controls who can send, read, and decrypt messages. Let's see how to create one:

✉️

Sending a Message

Sending a message is a three-step process that the SDK handles for you: encrypt the content with Seal, store the encrypted blob on Walrus, and record the blob reference on-chain. The sendMessage method takes a channel ID and your plaintext content, then performs all three steps atomically. Here's the complete flow:

📖

Reading Messages

Reading messages reverses the flow: fetch blob references from Sui, retrieve encrypted blobs from Walrus, and decrypt with Seal. Again, the SDK handles this. getMessages returns decrypted plaintext for all messages you're authorized to read. If you're not a member of the channel, decryption will fail — this is the Seal guarantee.

SDK Setup
📦

Installing the SDK

The messaging SDK has four packages: @mysten/messaging (the main client), @mysten/sui (blockchain interactions), @mysten/seal (encryption), and @mysten/walrus (storage). You install all four, then create each client and combine them into a MessagingClient. Let's walk through the complete setup:

🧩

Client Dependencies

Each client has a specific responsibility. SuiClient reads blockchain state and submits transactions. SealClient manages threshold encryption keys — it splits the encryption key across multiple servers so no single server can decrypt alone. WalrusClient stores and retrieves encrypted data blobs using erasure coding for durability. MessagingClient ties them together. Match each client to what it does: