backbone-queue 2.7.36

Backbone Framework Queue Module - Redis, SQS, and RabbitMQ support
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! RabbitMQ Real-Time Chat Example
//!
//! This example demonstrates a real-time chat system using RabbitMQ
//! Features shown:
//! - Real-time message broadcasting
//! - Chat rooms and user management
//! - Message persistence
//! - Online user tracking
//! - Message history retrieval
//!
//! Run with: cargo run --example rabbitmq_realtime_chat

use backbone_queue::{
    rabbitmq_simple::{RabbitMQQueueSimple, RabbitMQConfig, ExchangeType},
    traits::QueueService,
    types::{QueueMessage, QueuePriority},
    utils::rabbitmq_simple::*,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ChatMessage {
    id: String,
    room_id: String,
    user_id: String,
    username: String,
    content: String,
    timestamp: String,
    message_type: String, // "text", "join", "leave", "system"
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ChatRoom {
    id: String,
    name: String,
    description: String,
    created_at: String,
    member_count: u32,
    max_members: u32,
    is_private: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct UserStatus {
    user_id: String,
    username: String,
    status: String, // "online", "offline", "away"
    last_seen: String,
    current_room: Option<String>,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    env_logger::init();

    println!("💬 RabbitMQ Real-Time Chat System");
    println!("=================================");

    // Setup chat infrastructure
    setup_chat_infrastructure().await?;

    // Example 1: Create and join chat room
    println!("🏠 Example 1: Chat Room Management");
    chat_room_management().await?;
    println!();

    // Example 2: Real-time messaging
    println!("⚡ Example 2: Real-Time Messaging");
    real_time_messaging().await?;
    println!();

    // Example 3: User presence tracking
    println!("👥 Example 3: User Presence Tracking");
    user_presence_tracking().await?;
    println!();

    // Example 4: Message history and search
    println!("📚 Example 4: Message History & Search");
    message_history().await?;
    println!();

    // Example 5: Private messaging
    println!("🔒 Example 5: Private Messaging");
    private_messaging().await?;
    println!();

    // Example 6: System notifications
    println!("📢 Example 6: System Notifications");
    system_notifications().await?;
    println!();

    println!("✅ Chat system examples completed!");
    Ok(())
}

async fn setup_chat_infrastructure() -> Result<(), Box<dyn std::error::Error>> {
    println!("   🏗️  Setting up chat infrastructure...");

    // Main chat messages exchange (fanout for broadcasting)
    let messages_queue = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "chat.messages".to_string(),
            exchange_name: "chat.messages.fanout".to_string(),
            exchange_type: ExchangeType::Fanout,
            routing_key: None,
        }
    ).await?;

    // Room management (topic exchange for room-specific messages)
    let room_queue = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "chat.rooms".to_string(),
            exchange_name: "chat.rooms.topic".to_string(),
            exchange_type: ExchangeType::Topic,
            routing_key: Some("room.*".to_string()),
        }
    ).await?;

    // User presence tracking
    let presence_queue = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "chat.presence".to_string(),
            exchange_name: "chat.presence.direct".to_string(),
            exchange_type: ExchangeType::Direct,
            routing_key: Some("presence.update".to_string()),
        }
    ).await?;

    // Private messages (direct exchange for 1-to-1 messaging)
    let private_queue = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "chat.private".to_string(),
            exchange_name: "chat.private.direct".to_string(),
            exchange_type: ExchangeType::Direct,
            routing_key: Some("private.message".to_string()),
        }
    ).await?;

    // System notifications (high priority)
    let notifications_queue = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "chat.notifications".to_string(),
            exchange_name: "chat.notifications.direct".to_string(),
            exchange_type: ExchangeType::Direct,
            routing_key: Some("notification.urgent".to_string()),
        }
    ).await?;

    println!("   ✓ Chat Messages Exchange: chat.messages.fanout");
    println!("   ✓ Room Management Exchange: chat.rooms.topic");
    println!("   ✓ Presence Tracking: chat.presence.direct");
    println!("   ✓ Private Messages: chat.private.direct");
    println!("   ✓ System Notifications: chat.notifications.direct");
    println!("   ✓ Chat infrastructure setup complete!\n");

    Ok(())
}

async fn chat_room_management() -> Result<(), Box<dyn std::error::Error>> {
    println!("   🏠 Managing chat rooms...");

    // Create public chat room
    let public_room = ChatRoom {
        id: "room_general".to_string(),
        name: "General Discussion".to_string(),
        description: "Open discussion room for all topics".to_string(),
        created_at: chrono::Utc::now().to_rfc3339(),
        member_count: 0,
        max_members: 100,
        is_private: false,
    };

    let room_queue = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "room_management".to_string(),
            exchange_name: "chat.rooms.topic".to_string(),
            exchange_type: ExchangeType::Topic,
            routing_key: Some("room.create".to_string()),
        }
    ).await?;

    let room_message = QueueMessage::builder()
        .payload(serde_json::to_value(public_room)?)
        .expect("Failed to serialize room data")
        .priority(QueuePriority::Normal)
        .routing_key("room.create")
        .build();

    let room_id = room_queue.enqueue(room_message).await?;
    println!("   ✓ Created public room: {}", public_room.name);

    // Create private chat room
    let private_room = ChatRoom {
        id: "room_project_alpha".to_string(),
        name: "Project Alpha".to_string(),
        description: "Private room for Project Alpha team members".to_string(),
        created_at: chrono::Utc::now().to_rfc3339(),
        member_count: 0,
        max_members: 10,
        is_private: true,
    };

    let private_message = QueueMessage::builder()
        .payload(serde_json::to_value(private_room)?)
        .expect("Failed to serialize private room data")
        .priority(QueuePriority::High)
        .routing_key("room.create.private")
        .build();

    let private_id = room_queue.enqueue(private_message).await?;
    println!("   ✓ Created private room: {}", private_room.name);

    // Simulate room join
    simulate_room_join(&room_queue, &public_room.id).await?;
    simulate_room_join(&room_queue, &private_room.id).await?;

    Ok(())
}

async fn simulate_room_join(
    queue: &RabbitMQQueueSimple,
    room_id: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let join_message = ChatMessage {
        id: format!("join_{}", chrono::Utc::now().timestamp_millis()),
        room_id: room_id.to_string(),
        user_id: "user_12345".to_string(),
        username: "Alice".to_string(),
        content: "has joined the room".to_string(),
        timestamp: chrono::Utc::now().to_rfc3339(),
        message_type: "join".to_string(),
    };

    let message = QueueMessage::builder()
        .payload(serde_json::to_value(join_message)?)
        .expect("Failed to serialize join message")
        .priority(QueuePriority::Normal)
        .routing_key(format!("room.{}", room_id))
        .build();

    let message_id = queue.enqueue(message).await?;
    println!("   👤 User joined room {}: {}", room_id, message_id);
    Ok(())
}

async fn real_time_messaging() -> Result<(), Box<dyn std::error::Error>> {
    println!("   ⚡ Demonstrating real-time messaging...");

    let messages_queue = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "chat.messages.general".to_string(),
            exchange_name: "chat.messages.fanout".to_string(),
            exchange_type: ExchangeType::Fanout,
            routing_key: None,
        }
    ).await?;

    // Simulate multiple users sending messages
    let users = vec![
        ("user_12345", "Alice"),
        ("user_67890", "Bob"),
        ("user_11111", "Charlie"),
    ];

    let messages = vec![
        "Hey everyone! How's it going?",
        "Working on a new feature today 🚀",
        "Has anyone tried the new RabbitMQ implementation?",
        "It's really powerful! Great for microservices 🐰",
        "We're using it for our chat system!",
        "Love the topic exchange for routing!",
    ];

    for (i, ((user_id, username), content)) in users.iter().zip(&messages).enumerate() {
        let chat_message = ChatMessage {
            id: format!("msg_{}", chrono::Utc::now().timestamp_millis() + i as i64),
            room_id: "room_general".to_string(),
            user_id: user_id.to_string(),
            username: username.to_string(),
            content: content.to_string(),
            timestamp: chrono::Utc::now().to_rfc3339(),
            message_type: "text".to_string(),
        };

        let message = QueueMessage::builder()
            .payload(serde_json::to_value(chat_message)?)
            .expect("Failed to serialize chat message")
            .priority(QueuePriority::Normal)
            .build();

        let message_id = messages_queue.enqueue(message).await?;
        println!("   💬 {}: {} ({})", username, content, message_id);

        // Small delay between messages
        tokio::time::sleep(Duration::from_millis(200)).await;
    }

    println!("   📢 All messages broadcast to connected users");
    Ok(())
}

async fn user_presence_tracking() -> Result<(), Box<dyn std::error::Error>> {
    println!("   👥 Tracking user presence...");

    let presence_queue = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "presence.tracking".to_string(),
            exchange_name: "chat.presence.direct".to_string(),
            exchange_type: ExchangeType::Direct,
            routing_key: Some("presence.update".to_string()),
        }
    ).await?;

    // User comes online
    let user_online = UserStatus {
        user_id: "user_12345".to_string(),
        username: "Alice".to_string(),
        status: "online".to_string(),
        last_seen: chrono::Utc::now().to_rfc3339(),
        current_room: Some("room_general".to_string()),
    };

    let online_message = QueueMessage::builder()
        .payload(serde_json::to_value(user_online)?)
        .expect("Failed to serialize user status")
        .priority(QueuePriority::High)
        .routing_key("presence.update")
        .build();

    let online_id = presence_queue.enqueue(online_message).await?;
    println!("   👤 User Alice is now online: {}", online_id);

    // Simulate other users coming online
    let other_users = vec![
        ("user_67890", "Bob"),
        ("user_11111", "Charlie"),
        ("user_22222", "David"),
    ];

    for (user_id, username) in other_users {
        let status = UserStatus {
            user_id: user_id.to_string(),
            username: username.to_string(),
            status: "online".to_string(),
            last_seen: chrono::Utc::now().to_rfc3339(),
            current_room: Some("room_general".to_string()),
        };

        let message = QueueMessage::builder()
            .payload(serde_json::to_value(status)?)
            .expect("Failed to serialize user status")
            .priority(QueuePriority::High)
            .routing_key("presence.update")
            .build();

        let message_id = presence_queue.enqueue(message).await?;
        println!("   👤 User {} is now online: {}", username, message_id);
    }

    // User goes away
    tokio::time::sleep(Duration::from_millis(500)).await;

    let user_away = UserStatus {
        user_id: "user_12345".to_string(),
        username: "Alice".to_string(),
        status: "away".to_string(),
        last_seen: chrono::Utc::now().to_rfc3339(),
        current_room: Some("room_general".to_string()),
    };

    let away_message = QueueMessage::builder()
        .payload(serde_json::to_value(user_away)?)
        .expect("Failed to serialize user status")
        .priority(QueuePriority::Normal)
        .routing_key("presence.update")
        .build();

    let away_id = presence_queue.enqueue(away_message).await?;
    println!("   😴 User Alice is now away: {}", away_id);

    println!("   📊 Current online users: 4");

    Ok(())
}

async fn message_history() -> Result<(), Box<dyn std::error::Error>> {
    println!("   📚 Retrieving message history...");

    // In a real implementation, this would query a database
    // For this example, we'll simulate historical messages

    let historical_messages = vec![
        ("msg_001", "2023-10-01T10:00:00Z", "John", "Welcome to the chat!"),
        ("msg_002", "2023-10-01T10:01:15Z", "Jane", "Thanks! Excited to be here"),
        ("msg_003", "2023-10-01T10:02:30Z", "Bob", "What brings everyone here?"),
        ("msg_004", "2023-10-01T10:03:45Z", "John", "Working on a RabbitMQ project"),
        ("msg_005", "2023-10-01T10:05:00Z", "Jane", "That sounds interesting!"),
    ];

    println!("   📜 Message History (Last 20 minutes):");

    for (id, timestamp, username, content) in historical_messages {
        println!("     [{}] {}: {}", timestamp, username, content);
        println!("       Message ID: {}", id);
    }

    // Simulate message search
    println!("\n   🔍 Searching for 'RabbitMQ' in messages...");

    let search_results: Vec<_> = historical_messages
        .iter()
        .filter(|(_, _, _, content)| content.contains("RabbitMQ"))
        .collect();

    println!("   📊 Search Results:");
    if search_results.is_empty() {
        println!("     No messages found containing 'RabbitMQ'");
    } else {
        for (id, timestamp, username, content) in search_results {
            println!("     ✓ [{}] {}: {}", timestamp, username, content);
        }
    }

    Ok(())
}

async fn private_messaging() -> Result<(), Box<dyn std::error::Error>> {
    println!("   🔒 Setting up private messaging...");

    let private_queue = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "private.messages.alice_bob".to_string(),
            exchange_name: "chat.private.direct".to_string(),
            exchange_type: ExchangeType::Direct,
            routing_key: Some("private.message.bob".to_string()), // Route to Bob's private queue
        }
    ).await?;

    // Alice sends a private message to Bob
    let private_message = ChatMessage {
        id: format!("private_{}", chrono::Utc::now().timestamp_millis()),
        room_id: "private_alice_bob".to_string(),
        user_id: "user_12345".to_string(),
        username: "Alice".to_string(),
        content: "Hey Bob, wanted to ask you about the RabbitMQ implementation...".to_string(),
        timestamp: chrono::Utc::now().to_rfc3339(),
        message_type: "private".to_string(),
    };

    let message = QueueMessage::builder()
        .payload(serde_json::to_value(private_message)?)
        .expect("Failed to serialize private message")
        .priority(QueuePriority::Normal)
        .routing_key("private.message.bob")
        .build();

    let message_id = private_queue.enqueue(message).await?;
    println!("   💬 Alice sent private message to Bob: {}", message_id);

    // Bob's reply
    tokio::time::sleep(Duration::from_millis(300)).await;

    let bob_reply = ChatMessage {
        id: format!("private_{}", chrono::Utc::now().timestamp_millis()),
        room_id: "private_alice_bob".to_string(),
        user_id: "user_67890".to_string(),
        username: "Bob".to_string(),
        content: "Hey Alice! Sure, what would you like to know? 🐰".to_string(),
        timestamp: chrono::Utc::now().to_rfc3339(),
        message_type: "private".to_string(),
    };

    let reply = QueueMessage::builder()
        .payload(serde_json::to_value(bob_reply)?)
        .expect("Failed to serialize reply")
        .priority(QueuePriority::Normal)
        .routing_key("private.message.alice") // Route back to Alice's queue
        .build();

    let reply_id = private_queue.enqueue(reply).await?;
    println!("   💬 Bob replied to Alice: {}", reply_id);

    println!("   🔒 Private conversation established between Alice and Bob");
    Ok(())
}

async fn system_notifications() -> Result<(), Box<dyn std::error::Error>> {
    println!("   📢 Sending system notifications...");

    let notifications_queue = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "system.notifications".to_string(),
            exchange_name: "chat.notifications.direct".to_string(),
            exchange_type: ExchangeType::Direct,
            routing_key: Some("notification.urgent".to_string()),
        }
    ).await?;

    // System maintenance notification
    let maintenance_notification = ChatMessage {
        id: format!("sys_{}", chrono::Utc::now().timestamp_millis()),
        room_id: "system".to_string(),
        user_id: "system".to_string(),
        username: "System".to_string(),
        content: "🔧 Scheduled maintenance in 10 minutes. Chat services will be temporarily unavailable.".to_string(),
        timestamp: chrono::Utc::now().to_rfc3339(),
        message_type: "system".to_string(),
    };

    let notification = QueueMessage::builder()
        .payload(serde_json::to_value(maintenance_notification)?)
        .expect("Failed to serialize system notification")
        .priority(QueuePriority::Critical)
        .routing_key("notification.urgent")
        .build();

    let notification_id = notifications_queue.enqueue(notification).await?;
    println!("   📢 System notification sent: {}", notification_id);

    // Welcome message for new users
    tokio::time::sleep(Duration::from_millis(200)).await;

    let welcome_notification = ChatMessage {
        id: format!("welcome_{}", chrono::Utc::now().timestamp_millis()),
        room_id: "system".to_string(),
        user_id: "system".to_string(),
        username: "System".to_string(),
        content: "👋 Welcome to the chat! Please read the community guidelines before posting.".to_string(),
        timestamp: chrono::Utc::now().to_rfc3339(),
        message_type: "system".to_string(),
    };

    let welcome = QueueMessage::builder()
        .payload(serde_json::to_value(welcome_notification)?)
        .expect("Failed to serialize welcome notification")
        .priority(QueuePriority::Normal)
        .routing_key("notification.info")
        .build();

    let welcome_id = notifications_queue.enqueue(welcome).await?;
    println!("   👋 Welcome notification sent: {}", welcome_id);

    // User alert notification
    tokio::time::sleep(Duration::from_millis(200)).await();

    let alert_notification = ChatMessage {
        id: format!("alert_{}", chrono::Utc::now().timestamp_millis()),
        room_id: "system".to_string(),
        user_id: "moderator".to_string(),
        username: "Moderator".to_string(),
        content: "⚠️ Reminder: Please keep conversations respectful and on-topic.".to_string(),
        timestamp: chrono::Utc::now().to_rfc3339(),
        message_type: "system".to_string(),
    };

    let alert = QueueMessage::builder()
        .payload(serde_json::to_value(alert_notification)?)
        .expect("Failed to serialize alert notification")
        .priority(QueuePriority::High)
        .routing_key("notification.warning")
        .build();

    let alert_id = notifications_queue.enqueue(alert).await?;
    println!("   ⚠️ Moderator alert sent: {}", alert_id);

    println!("   📊 All notifications queued successfully");
    Ok(())
}

// Add this to your Cargo.toml dependencies:
// chrono = { version = "0.4", features = ["serde"] }