alepha@docs:~/docs/reference/primitives$
cat $subscriber.md
1 min read

#$subscriber

#Import

typescript
1import { $subscriber } from "alepha/topic";

#Overview

Creates a subscriber primitive to listen for messages from a specific topic.

Provides a dedicated message subscriber that connects to a topic and processes messages with custom handler logic, enabling scalable pub/sub architectures where multiple subscribers can react to the same events independently.

Key Features

  • Seamless integration with any $topic primitive
  • Full type safety inherited from topic schema
  • Real-time message delivery when events are published
  • Error isolation between subscribers
  • Support for multiple independent subscribers per topic

Common Use Cases

  • Notification services and audit logging
  • Analytics and metrics collection
  • Data synchronization and real-time UI updates

#Options

Option Type Required Description
topic TopicPrimitive<T> Yes The topic primitive that this subscriber will listen to for messages
handler TopicHandler<T> Yes Message handler function that processes individual messages from the topic

#Examples

ts
 1class UserActivityService { 2  userEvents = $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  });12 13  activityLogger = $subscriber({14    topic: this.userEvents,15    handler: async (message) => {16      const { userId, action, timestamp } = message.payload;17      await this.auditLogger.log({18        userId,19        action,20        timestamp21      });22    }23  });24 25  async trackUserLogin(userId: string) {26    await this.userEvents.publish({27      userId,28      action: "login",29      timestamp: Date.now()30    });31  }32}