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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! Redis adapter — streaming Pub/Sub reads (`SUBSCRIBE`) and writes (`PUBLISH`).
//!
//! Provides two transports, each with a producer and a consumer:
//!
//! **Pub/Sub** (fire-and-forget channels):
//! - [`redis_sub`] — producer that subscribes to a channel and emits each message as an event
//! - [`redis_pub`] — consumer that publishes messages to Redis channels
//!
//! **Streams** (a persistent, replayable log):
//! - [`redis_stream_read`] — producer that emits a snapshot of existing entries
//! followed by live entries as they are appended
//! - [`redis_stream_write`] — consumer that appends entries to a stream via `XADD`
//!
//! Redis Pub/Sub is **fire-and-forget**: messages are delivered only to clients
//! subscribed at the moment of publication. There is no backlog, no replay, and no
//! offsets — a subscriber sees only what is published after its `SUBSCRIBE` completes.
//! Redis Streams, by contrast, persist entries with monotonic IDs, so
//! [`redis_stream_read`] can replay history before tailing live appends.
//!
//! # Setup
//!
//! ## Local (Docker)
//!
//! ```sh
//! docker run --rm -p 6379:6379 redis:7-alpine
//! ```
//!
//! # Subscribing to a channel
//!
//! [`redis_sub`] subscribes to a single channel and streams every message published to it
//! as a [`RedisEvent`].
//!
//! ```ignore
//! use wingfoil::adapters::redis::*;
//! use wingfoil::*;
//!
//! let conn = RedisConnection::new("redis://127.0.0.1:6379");
//!
//! redis_sub(conn, "prices")
//! .collapse()
//! .for_each(|event, _| {
//! println!("{}: {:?}", event.channel, event.payload_str())
//! })
//! .run(RunMode::RealTime, RunFor::Forever)
//! .unwrap();
//! ```
//!
//! # Publishing messages
//!
//! [`redis_pub`] (or the fluent `.redis_pub()` method) consumes a `Burst<RedisEntry>`
//! stream and publishes each entry to the channel it names.
//!
//! ```ignore
//! use wingfoil::adapters::redis::*;
//! use wingfoil::*;
//!
//! let conn = RedisConnection::new("redis://127.0.0.1:6379");
//!
//! constant(burst![
//! RedisEntry { channel: "prices".into(), payload: b"42".to_vec() },
//! ])
//! .redis_pub(conn)
//! .run(RunMode::RealTime, RunFor::Cycles(1))
//! .unwrap();
//! ```
//!
//! # Reading and writing streams
//!
//! [`redis_stream_read`] first emits all existing entries under the stream key
//! (via `XRANGE`), capturing the last entry ID, then tails live appends (via
//! `XREAD BLOCK` from that ID) so no entry is missed in the handoff.
//!
//! ```ignore
//! use wingfoil::adapters::redis::*;
//! use wingfoil::*;
//!
//! let conn = RedisConnection::new("redis://127.0.0.1:6379");
//!
//! // Append an entry.
//! constant(burst![RedisStreamRecord::single("events", "kind", b"login".to_vec())])
//! .redis_stream_write(conn.clone())
//! .run(RunMode::RealTime, RunFor::Cycles(1))
//! .unwrap();
//!
//! // Replay history, then tail live entries.
//! redis_stream_read(conn, "events")
//! .collapse()
//! .for_each(|event, _| println!("{} {:?}", event.id, event.fields))
//! .run(RunMode::RealTime, RunFor::Forever)
//! .unwrap();
//! ```
//!
//! # Round-trip example
//!
//! See [`examples/redis`](https://github.com/wingfoil-io/wingfoil/tree/main/wingfoil/examples/redis)
//! for a full working example that publishes messages, subscribes to them, transforms
//! the payloads, and republishes the results to a second channel.
pub use *;
pub use *;
pub use *;
/// Connection configuration for Redis.
/// A message to publish to a Redis channel.
/// A message received from a subscribed Redis channel.
/// A record to append to a Redis stream via `XADD`.
///
/// A Redis stream entry is an ordered map of field → value pairs. The entry ID is
/// assigned by Redis (`XADD key *`), so it is not part of the record.
/// An entry read from a Redis stream via `XRANGE` (snapshot) or `XREAD` (tail).