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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! WebSocket API exposure for the framework
//!
//! This module provides WebSocket-specific routing and real-time event handling.
//! It is completely separate from the core framework logic and follows the same
//! pattern as RestExposure and GraphQLExposure.
//!
//! # Architecture
//!
//! ```text
//! Client ──ws──▶ /ws ──▶ ws_handler() ──▶ ConnectionManager
//! │
//! subscribe(filter)
//! │
//! EventBus ──broadcast──▶ filter ──▶ Client
//! ```
//!
//! # Sink Integration
//!
//! When a `SinkRegistry` is configured on the host, `build_router()` automatically
//! wires a `WebSocketSink` for every sink of type `WebSocket` declared in the YAML
//! config. The sink dispatches notifications through the `ConnectionManagerDispatcher`.
//!
//! ```text
//! FlowRuntime → DeliverOp → SinkRegistry → WebSocketSink
//! └─ ConnectionManagerDispatcher
//! └─ ConnectionManager
//! └─ send_to_user() / broadcast_payload()
//! ```
//!
//! # Protocol
//!
//! Client → Server (JSON):
//! - `{"type": "subscribe", "filter": {"entity_type": "order"}}`
//! - `{"type": "unsubscribe", "subscription_id": "..."}`
//! - `{"type": "ping"}`
//!
//! Server → Client (JSON):
//! - `{"type": "event", "data": {...}}`
//! - `{"type": "subscribed", "subscription_id": "..."}`
//! - `{"type": "unsubscribed", "subscription_id": "..."}`
//! - `{"type": "notification", "data": {...}}`
//! - `{"type": "pong"}`
//! - `{"type": "error", "message": "..."}`
use crateSinkType;
use crateSink;
use crateWebSocketSink;
use crateServerHost;
use Result;
use ;
use Arc;
/// WebSocket API exposure implementation
///
/// This struct encapsulates all WebSocket-specific logic for exposing real-time
/// events from the framework. It consumes a `ServerHost` and produces an Axum
/// router with a `/ws` endpoint.
///
/// # Requirements
///
/// The `ServerHost` must have an `EventBus` configured (via `ServerBuilder::with_event_bus()`)
/// for the WebSocket exposure to function. Without an EventBus, the WebSocket endpoint
/// will still accept connections but no events will be broadcast.
///
/// # Sink Auto-wiring
///
/// If the host has a `SinkRegistry` and YAML config declares sinks of type `websocket`,
/// `build_router()` will automatically register a `WebSocketSink` backed by the
/// `ConnectionManager` for each such sink. This enables the `deliver` operator in
/// event pipelines to dispatch payloads to connected WebSocket clients.
///
/// # Example
///
/// ```rust,ignore
/// use this::server::{ServerBuilder, WebSocketExposure, RestExposure};
/// use this::storage::InMemoryLinkService;
/// use std::sync::Arc;
///
/// let host = Arc::new(
/// ServerBuilder::new()
/// .with_link_service(InMemoryLinkService::new())
/// .with_event_bus(1024)
/// .register_module(my_module)?
/// .build_host()?
/// );
///
/// let rest_router = RestExposure::build_router(host.clone(), vec![])?;
/// let ws_router = WebSocketExposure::build_router(host)?;
///
/// let app = rest_router.merge(ws_router);
/// ```
;