# Messages

Code on this page comes in TypeScript, Rust, Python, and Go. The same page with one language only: [Python](https://docs.laserdata.com/md/python/laser-sdk/log.md), [TypeScript](https://docs.laserdata.com/md/typescript/laser-sdk/log.md), [Rust](https://docs.laserdata.com/md/rust/laser-sdk/log.md), [Go](https://docs.laserdata.com/md/go/laser-sdk/log.md).

A topic is a durable, ordered log of messages. Producers append to it, and any number of readers can read it live, resume from a saved position, or replay it from the start. Use it for agent events, tool-call records, telemetry, and any feed that more than one service needs.

## Quick example

An agent records each tool call it makes. An auditor reads every record back as a typed value.

**TypeScript**

```ts
import { Json, Laser } from "@laserdata/laser-sdk"

interface ToolCall {
  readonly agent: string
  readonly tool: string
  readonly ms: number
}

// Types do not exist at runtime, so the codec checks each decoded value.
const TOOL_CALL = new Json<ToolCall>((value) => {
  const { agent, tool, ms } = value as Record<string, unknown>
  if (typeof agent !== "string" || typeof tool !== "string" || typeof ms !== "number") {
    throw new TypeError("not a tool call")
  }
  return { agent, tool, ms }
})

await using laser = await Laser.connectEnv()
const topic = laser.stream("agents").topic("tool-calls")
await topic.ensure(2)

const calls = topic.json(TOOL_CALL)
await calls.publish({ agent: "researcher", tool: "search", ms: 120 })
await calls.publish({ agent: "researcher", tool: "summarize", ms: 840 })

const reader = await calls.records("audit")
for (let result = await reader.next(); result !== undefined; result = await reader.next()) {
  if (result.kind === "error") throw result.error
  const call = result.record.value
  console.log(call.agent, call.tool, call.ms)
}
```

**Rust**

```rust
use laser_sdk::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct ToolCall {
    agent: String,
    tool: String,
    ms: u32,
}

#[tokio::main]
async fn main() -> Result<(), LaserError> {
    let laser = Laser::connect_env().await?;
    let topic = laser.stream("agents").topic("tool-calls");
    topic.ensure(2, None).await?;

    let calls = topic.json::<ToolCall>();
    for (tool, ms) in [("search", 120), ("summarize", 840)] {
        let call = ToolCall { agent: "researcher".into(), tool: tool.into(), ms };
        calls.publish(&call)?.send().await?;
    }

    let mut reader = calls.records("audit")?;
    while let Some(next) = reader.next().await {
        let call = next?.value;
        println!("{} {} {}", call.agent, call.tool, call.ms);
    }
    Ok(())
}
```

**Python**

```python
import asyncio
from dataclasses import dataclass

import laser_sdk as ls


@dataclass
class ToolCall:
    agent: str
    tool: str
    ms: int


async def main() -> None:
    laser = await ls.Laser.connect_env()
    topic = laser.stream("agents").topic("tool-calls")
    await topic.ensure(2)

    calls = topic.json(ToolCall)
    for tool, ms in (("search", 120), ("summarize", 840)):
        await calls.publish(ToolCall("researcher", tool, ms)).send()

    reader = calls.records("audit")
    while (record := await reader.next()) is not None:
        print(record.value.agent, record.value.tool, record.value.ms)
    await laser.close()


asyncio.run(main())
```

**Go**

```go
package main

import (
    "context"
    "errors"
    "fmt"
    "os"
    "time"

    "github.com/laserdata/laser-sdk/foreign/go/laser"
)

type ToolCall struct {
    Agent string `json:"agent"`
    Tool  string `json:"tool"`
    MS    int    `json:"ms"`
}

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
    defer cancel()
    if err := run(ctx); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}

func run(ctx context.Context) error {
    client, err := laser.ConnectEnv(ctx)
    if err != nil {
        return err
    }
    defer client.Close()
    topic := client.Stream("agents").Topic("tool-calls")
    if err := topic.Ensure(ctx, 2, nil); err != nil {
        return err
    }

    calls := laser.TypedJSON[ToolCall](topic)
    for _, call := range []ToolCall{{"researcher", "search", 120}, {"researcher", "summarize", 840}} {
        if _, err := calls.Publish(call).Send(ctx); err != nil {
            return err
        }
    }

    reader, err := topic.ReplayAs("audit")
    if err != nil {
        return err
    }
    var undecoded error
    err = calls.Records(ctx, reader, func(record *laser.TypedRecord[ToolCall]) bool {
        fmt.Println(record.Body.Agent, record.Body.Tool, record.Body.MS)
        return true
    }, func(err *laser.TypedDecodeError) { undecoded = errors.Join(undecoded, err) })
    return errors.Join(err, undecoded)
}
```

`topic.ensure(2)` creates the topic with two partitions if it does not exist. Its [replication factor](/laser-sdk/advanced/log#replication-factor), the number of nodes that store each partition, is the server's default unless you pass one: `ensure(2, Some(3))` in Rust, where `None` keeps the default, `ensure(2, replication_factor=3)` in Python, and `ensure(2, 3)` in TypeScript.

`topic.json(..)` returns a typed handle. Its `publish` encodes the value and its `records` reader decodes it. The reader starts at offset 0. When it reaches the end of the topic, `next()` returns `None` in Rust and Python and `undefined` in TypeScript.

## Process messages live in a consumer group

A consumer group shares a topic's partitions across processes. Each partition goes to one member at a time. The server stores the group's progress, so a restarted member resumes where the group left off. Commit a message after you finish handling it.

**TypeScript**

```ts
await using consumer = await topic.consumerGroup("auditors").consumer({
  commitPolicy: { kind: "disabled" },
  startAt: { kind: "first" }
})

for await (const message of consumer) {
  const call = TOOL_CALL.decode(message.payload)
  console.log(message.position.offset, call.tool)
  await consumer.commit(message)
}
```

**Rust**

```rust
let mut consumer = topic
    .consumer_group("auditors")
    .consumer()
    .commit_policy(CommitPolicy::Disabled)
    .start_at(ConsumerStart::First)
    .build()
    .await?;

while let Some(message) = consumer.next().await {
    let message = message?;
    let call: ToolCall = message.json()?;
    println!("{} {}", message.position.offset, call.tool);
    consumer.commit(&message).await?;
}
consumer.shutdown().await?;
```

**Python**

```python
consumer = topic.consumer_group("auditors").consumer(
    auto_commit="disabled",
    polling="first",
)

async for message in consumer:
    call = message.json()
    print(message.position.offset, call["tool"])
    await consumer.commit(message)

await consumer.shutdown()
```

**Go**

```go
consumer, err := topic.Group("auditors").Consumer(ctx, laser.ConsumerOptions{ManualCommit: true})
if err != nil {
    return err
}
defer consumer.Close(context.WithoutCancel(ctx))

return consumer.Each(ctx, func(ctx context.Context, record *laser.Record) error {
    var call ToolCall
    if err := json.Unmarshal(record.Payload(), &call); err != nil {
        return err
    }
    fmt.Println(record.Offset(), call.Tool)
    return consumer.Commit(ctx, record.Checkpoint)
})
```

The loop waits for new messages until you stop it. The server holds each read until a message arrives or 5 seconds pass, so an idle consumer sends few requests and a new message arrives without delay. To commit automatically, pick another commit policy. See [Consumers and commit policies](/laser-sdk/advanced/log#consumers-offsets-and-commit-policies).

## Keep related messages in order

Order holds only within one partition. Give messages that must stay in order the same key, such as an agent or session ID. The key picks the partition.

**TypeScript**

```ts
const key = new TextEncoder().encode("researcher")
await calls.publish({ agent: "researcher", tool: "search", ms: 120 }, { key })
```

**Rust**

```rust
let call = ToolCall { agent: "researcher".into(), tool: "search".into(), ms: 120 };
calls.publish(&call)?.partition_key("researcher").send().await?;
```

**Python**

```python
call = ToolCall("researcher", "search", 120)
await calls.publish(call).partition_key("researcher").send()
```

**Go**

```go
call := ToolCall{"researcher", "search", 120}
_, err := calls.Publish(call).Key("researcher").Send(ctx)
return err
```

## Resume a reader from saved offsets

A typed reader tracks the next offset of each partition. Save the offsets, then hand them back to resume after a restart. Without saved offsets, a reader starts at offset 0.

**TypeScript**

```ts
const saved = reader.offsets // Map of partition to bigint offset
const resumed = (await calls.records("audit")).fromOffsets(saved)
```

**Rust**

```rust
let saved = reader.offsets().to_vec(); // one offset per partition
let resumed = calls.records("audit")?.from_offsets(saved);
```

**Python**

```python
saved = reader.offsets  # one offset per partition
resumed = calls.records("audit", from_offsets=saved)
```

**Go**

```go
saved := reader.Offsets() // one offset per partition
resumed, err := topic.ReplayAs("audit")
if err != nil {
    return err
}
resumed = resumed.FromOffsets(saved)
```

A typed reader is a cursor that you own. It is not a consumer group, and the server does not store its offsets. Use a consumer group for production processing.

## Inspect streams and topics

`laser.streams()` lists the streams that you can read. `stream(name).topics()`, `topic.partitions()`, and `topic.consumer_groups()` (TypeScript `consumerGroups()`) list what a stream or a topic holds. `get()` reads one stream or topic, and returns no value when it does not exist. `stream(name).rename(new_name)` renames a stream, and `topic.update(..)` changes the name, the message expiry, or the size cap of a topic. See [Inspect streams and topics](/laser-sdk/advanced/log#inspect-streams-and-topics).

## Publish at volume

`publish` sends one message and waits for the server. For throughput, use a long-lived producer, a background producer, or a batching producer. See [Producing at volume](/laser-sdk/advanced/log#producing-at-volume).

## Good to know

* Delivery is at least once. A crash between handling and commit delivers the message again, so handlers must be safe to repeat.
* There is no order across partitions. More partitions give more parallel consumers. One key keeps its messages in order.
* When a group member joins or leaves, the server moves partitions between members. Each partition has one member at a time.
* A message that does not decode fails with a decode error that names its position, and the next read moves past it. In Python, an `async for` loop over a typed reader ends on that error, so use `next()` in a loop to skip it.
* Payloads are bytes. JSON, CBOR, MessagePack, BSON, and Avro helpers encode values into those bytes.
* Messages work on plain Apache Iggy, Laser Stack, and LaserData Cloud.

- [Messages in depth](/laser-sdk/advanced/log): Producers, batching, publish failures, commit policies, offsets, and statistics
- [Filters](/laser-sdk/consumer-filters): Let the server send each group only the messages it needs
- [Sessions](/laser-sdk/session): Record a unit of agent work on the log
- [Connect](/laser-sdk/connect): Connection strings, timeouts, and publish retries
- [log example](https://github.com/laserdata/laser-sdk/tree/main/examples/rust/src/log): Write and read back typed messages in Rust, Python, and TypeScript

Source: https://docs.laserdata.com/laser-sdk/log
