Stream Management

This guide covers how to manage data streams using the SuiPulse SDK.

Creating Streams

Basic Stream Creation

const result = await suiPulse.createStream({
  name: "My Stream",
  description: "A test data stream",
  metadata: new Uint8Array(Buffer.from("Initial metadata")),
  isPublic: true,
});

// Get the stream ID from the result
const streamId = result.streamId;

Stream with Custom Metadata

const metadata = {
  version: "1.0.0",
  tags: ["test", "example"],
  customField: "value",
};

const result = await suiPulse.createStream({
  name: "Custom Stream",
  description: "Stream with custom metadata",
  metadata: new Uint8Array(Buffer.from(JSON.stringify(metadata))),
  isPublic: false,
});

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 },
});

// Check results
console.log("Total updates:", result.summary.total);
console.log("Successful:", result.summary.succeeded);
console.log("Failed:", result.summary.failed);

Reading Stream Data

Get Stream Data

const streamData = await suiPulse.getDataStream(streamId);
console.log("Stream data:", streamData);

Get Snapshot Data

const snapshotData = await suiPulse.getSnapshotData(snapshotId);
console.log("Snapshot data:", snapshotData);

Stream Composition

Compose Streams

await suiPulse.composeStreams(parentStreamId, childStreamId);

Get Snapshot Stream ID

const streamId = await suiPulse.getSnapshotStreamId(snapshotId);

Permission Management

Add Permission

// Permission levels: 0 (read), 1 (write), 2 (admin)
await suiPulse.addPermission(streamId, address, 1);

Check Subscription

const isSubscribed = await suiPulse.isSubscribed(streamId, address);
console.log(`Address ${address} is ${isSubscribed ? "" : "not "}subscribed`);

Snapshot Management

Create Snapshot

const result = await suiPulse.createSnapshot(streamId, {
  metadata: "Snapshot metadata",
});

// Get snapshot ID from the event
const snapshotId = result.events.find(
  (event) => event.type === `${suiPulse.packageId}::storage::SnapshotCreated`
)?.parsedJson.snapshot_id;

Update Snapshot

await suiPulse.updateSnapshot(snapshotId, {
  data: new Uint8Array(Buffer.from("Updated snapshot data")),
});

Transfer Snapshot

await suiPulse.transferSnapshot(snapshotId, recipientAddress);

Best Practices

  1. Data Management

    • Use appropriate data formats
    • Handle large data efficiently
    • Implement proper error handling
  2. Batch Operations

    • Use batch operations for multiple updates
    • Handle partial failures gracefully
    • Monitor operation results
  3. Security

    • Set appropriate permission levels
    • Validate input data
    • Handle sensitive data securely

Next Steps