ServerEvents
Server Events
Publishing events from the server using Effect-TS PubSub.
Event Publishing
The server uses Effect-TS PubSub for in-memory event handling:
import { publishNow } from "@repo/crm-events/server";
// Publish an event
publishNow({
_tag: "Organization.Created",
organization: { id: "org_123", name: "Acme" },
member: { ... },
user: { ... },
createdAt: new Date(),
});Core API
publishNow
Fire-and-forget event publishing:
export const publishNow = (event: AppEvent) => {
// Best-effort with timeout (1s) and overflow handling
Effect.runFork(PubSub.publish(_pubsub, event).pipe(
Effect.timeout(1000),
Effect.catchAll((error) => ...),
));
};publishBatch
Publish multiple events efficiently:
publishBatch([
{ _tag: "Event1", ... },
{ _tag: "Event2", ... },
]);Event Types
Defined in types.ts:
type EventTag = "Organization.Created" | "Organization.Seed" | "*";
interface AppEvent {
_tag: EventTag;
createdAt: Date;
}PubSub Configuration
- Capacity: 10,000 events (bounded)
- Overflow: Dropped with warning
- Timeout: 1 second per publish
Usage in Hooks
Events are published from auth hooks:
// packages/crm/auth/src/index.ts
afterCreateOrganization: async ({ organization, member, user }) => {
publishNow({
_tag: "Organization.Created",
organization,
member,
user,
createdAt: new Date(),
});
},