-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrealtime-service.ts
More file actions
107 lines (96 loc) · 3.36 KB
/
Copy pathrealtime-service.ts
File metadata and controls
107 lines (96 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* IRealtimeService - Realtime / PubSub Service Contract
*
* Defines the interface for realtime event subscription and publishing
* in ObjectStack. Concrete implementations (WebSocket, SSE, Socket.IO, etc.)
* should implement this interface.
*
* Follows Dependency Inversion Principle - plugins depend on this interface,
* not on concrete realtime transport implementations.
*
* Aligned with CoreServiceName 'realtime' in core-services.zod.ts.
*/
/**
* A realtime event payload
*/
export interface RealtimeEventPayload {
/** Event type (e.g. 'record.created', 'record.updated') */
type: string;
/** Object name the event relates to */
object?: string;
/** Event data */
payload: Record<string, unknown>;
/** Timestamp (ISO 8601) */
timestamp: string;
}
/**
* Handler function for realtime event subscriptions
*/
export type RealtimeEventHandler = (event: RealtimeEventPayload) => void | Promise<void>;
/**
* Subscription options for filtering events
*/
export interface RealtimeSubscriptionOptions {
/** Object name to filter events for */
object?: string;
/** Event types to listen for */
eventTypes?: string[];
/** Additional filter conditions */
filter?: Record<string, unknown>;
}
/**
* Enhanced subscription filter for metadata and data events
*/
export interface RealtimeSubscriptionFilter {
/** Metadata type filter (object, view, agent, tool, etc.) */
type?: string;
/** Package ID filter */
packageId?: string;
/** Event types to listen for */
eventTypes?: string[];
/** Record ID filter (for data events) */
recordId?: string;
/** Field names filter (for data events) */
fields?: string[];
}
export interface IRealtimeService {
/**
* Publish an event to all subscribers
* @param event - The event to publish
*/
publish(event: RealtimeEventPayload): Promise<void>;
/**
* Subscribe to realtime events
* @param channel - Channel/topic name
* @param handler - Event handler function
* @param options - Optional subscription filters
* @returns Subscription identifier for unsubscribing
*/
subscribe(channel: string, handler: RealtimeEventHandler, options?: RealtimeSubscriptionOptions): Promise<string>;
/**
* Unsubscribe from a channel
* @param subscriptionId - Subscription identifier returned by subscribe()
*/
unsubscribe(subscriptionId: string): Promise<void>;
/**
* Handle an incoming HTTP upgrade request (WebSocket handshake)
* @param request - Standard Request object
* @returns Standard Response object
*/
handleUpgrade?(request: Request): Promise<Response>;
/**
* Subscribe to metadata events (convenience method)
* @param filter - Subscription filter
* @param handler - Event handler function
* @returns Subscription identifier for unsubscribing
*/
subscribeMetadata?(filter: RealtimeSubscriptionFilter, handler: RealtimeEventHandler): Promise<string>;
/**
* Subscribe to data events (convenience method)
* @param filter - Subscription filter
* @param handler - Event handler function
* @returns Subscription identifier for unsubscribing
*/
subscribeData?(filter: RealtimeSubscriptionFilter, handler: RealtimeEventHandler): Promise<string>;
}