Data Streams
Data streams are the fundamental building blocks of SuiPulse, enabling real-time data synchronization and state management on the Sui blockchain.
Overview
A data stream in SuiPulse is a sequence of data points that can be updated in real-time and synchronized across multiple clients. Each stream has:
- A unique identifier
- Metadata (name, description, custom fields)
- Current data state
- Version history
- Access control settings
Creating Streams
import { SuiPulse, Network } from "@suipulse/sdk";
import { Ed25519Keypair } from "@mysten/sui.js/keypairs/ed25519";
// Initialize SuiPulse
const keypair = Ed25519Keypair.fromSecretKey(/* your private key */);
const suiPulse = new SuiPulse(keypair, Network.MAINNET);
// Create a stream
const result = await suiPulse.createStream({
name: "My Stream",
description: "A test data stream",
metadata: new Uint8Array(Buffer.from("Initial metadata")),
isPublic: true,
});
const streamId = result.streamId;
Updating Streams
Single Update
await suiPulse.updateStream(streamId, {
data: new Uint8Array(Buffer.from("Updated stream data")),
});
Batch Updates
const result = await suiPulse.updateStreamsBatch({
updates: [
{
streamId: "0x123...",
data: new Uint8Array(Buffer.from("Update 1")),
},
{
streamId: "0x456...",
data: new Uint8Array(Buffer.from("Update 2")),
},
],
options: { parallel: true },
});
Reading Stream Data
const streamData = await suiPulse.getDataStream(streamId);
console.log("Stream data:", streamData);
Stream Composition
Streams can be composed to create hierarchical relationships:
// Create parent-child relationship
await suiPulse.composeStreams(parentStreamId, childStreamId);
Access Control
Streams can be public or private, with fine-grained permission control:
// Add permission for an address
await suiPulse.addPermission(streamId, address, permissionLevel);
// Check if an address is subscribed
const isSubscribed = await suiPulse.isSubscribed(streamId, address);
Best Practices
-
Data Structure
- Use consistent data formats
- Keep metadata concise
- Plan for future extensibility
-
Performance
- Use batch operations for multiple updates
- Implement proper error handling
- Monitor stream size and growth
-
Security
- Set appropriate access controls
- Validate input data
- Handle sensitive data securely
Use Cases
- Real-time data synchronization
- IoT device monitoring
- DeFi price feeds
- Social media feeds
- Gaming state management
Next Steps
- Learn about Snapshots
- Explore Access Control
- Check out Analytics