Postboy Help

Quick Start: PostboyCallbackMessage

A minimal working example of a callback message that returns a typed response.

1. Define the message

Create a class that extends PostboyCallbackMessage<T>.

import { PostboyCallbackMessage } from '@artstesh/postboy'; import { User } from './models/user.model'; export class GetUserQuery extends PostboyCallbackMessage<User | null> { static readonly ID = 'user.load'; constructor(public readonly userId: string) { super(); } }

2. Register a handler

Inside a service, register a command handler using regCmd.

import {AppPostboyService} from '@shared/services/app-postboy.service'; import {GetUserQuery} from './messages/queries/get-user.query'; export class UserService { constructor(private postboy: AppPostboyService) { this.postboy.exec(new ConnectMessage(GetUserQuery, new Subject<GetUserQuery>)); this.postboy.sub(GetUserQuery).subscribe((msg) => { // Synchronous or asynchronous logic const user = this.cache.get(msg.userId); msg.finish(user ?? null); }); } private cache = new Map<string, User>(); }

3. Fire the callback and consume the response

Any component or service can send the query and subscribe to the result.

import {Component} from '@angular/core'; import {AppPostboyService} from '@shared/services/app-postboy.service'; import {GetUserQuery} from './messages/queries/get-user.query'; @Component({ /* ... */}) export class UserProfileComponent { constructor(private postboy: AppPostboyService) { } loadUser(id: string): void { this.postboy.fireCallback(new GetUserQuery(id)).subscribe((user) => { if (user) { console.log('User loaded:', user.name); } else { console.log('User not found'); } }); } }

Key points

  • PostboyCallbackMessage<T> guarantees a typed response.

  • The handler must call msg.finish(result) to send the result back.

  • The sender receives the result via the observable returned by fireCallback.

24 апреля 2026