alepha@docs:~/docs/reference/primitives$
cat $subscriber.md | pretty
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  dateTime = $inject(DateTimeProvider); 3  4  userEvents = $topic({ 5    name: "user-activity", 6    schema: { 7      payload: z.object({ 8        userId: z.text(), 9        action: z.enum(["login", "logout", "purchase"]),10        timestamp: z.number()11      })12    }13  });14 15  activityLogger = $subscriber({16    topic: this.userEvents,17    handler: async (message) => {18      const { userId, action, timestamp } = message.payload;19      await this.auditLogger.log({20        userId,21        action,22        timestamp23      });24    }25  });26 27  async trackUserLogin(userId: string) {28    await this.userEvents.publish({29      userId,30      action: "login",31      timestamp: this.dateTime.nowMillis()32    });33  }34}