alepha@docs:~/docs/reference/primitives$
cat $topic.md1 min read
#$topic
#Import
typescript
1import { $topic } from "alepha/topic";
#Overview
Creates a topic primitive for publish/subscribe messaging and event-driven architecture.
Enables decoupled communication through a pub/sub pattern where publishers send messages and multiple subscribers receive them. Supports type-safe messages, real-time delivery, event filtering, and pluggable backends (memory, Redis, custom providers).
Use Cases: User notifications, real-time chat, event broadcasting, microservice communication
#Options
| Option | Type | Required | Description |
|---|---|---|---|
name |
string |
No | Unique name identifier for the topic |
description |
string |
No | Human-readable description of the topic's purpose and usage |
provider |
"memory" | Service<TopicProvider> |
No | Topic provider configuration for message storage and delivery |
schema |
T |
Yes | TypeBox schema defining the structure of messages published to this topic |
handler |
TopicHandler<T> |
No | Default subscriber handler function that processes messages published to this topic |
#Examples
ts
1class NotificationService { 2 userActivity = $topic({ 3 name: "user-activity", 4 schema: { 5 payload: t.object({ 6 userId: t.text(), 7 action: t.enum(["login", "logout", "purchase"]), 8 timestamp: t.number() 9 })10 },11 handler: async (message) => {12 console.log(`User ${message.payload.userId}: ${message.payload.action}`);13 }14 });15 16 async trackLogin(userId: string) {17 await this.userActivity.publish({ userId, action: "login", timestamp: Date.now() });18 }19 20 async subscribeToEvents() {21 await this.userActivity.subscribe(async (message) => {22 // Additional subscriber logic23 });24 }25}