-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.cpp
More file actions
70 lines (56 loc) · 1.92 KB
/
Copy pathpublish.cpp
File metadata and controls
70 lines (56 loc) · 1.92 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
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <thread>
#include <nlohmann/json.hpp>
#include "kinopio/kinopio.hpp"
namespace {
std::string resolveServer() {
if (const char* value = std::getenv("KINOPIO_NATS_URL")) {
return value;
}
return "nats://demo.nats.io:4222";
}
} // namespace
int main() {
kinopio::KinopioOptions options;
options.servers = {resolveServer()};
kinopio::KinopioHub hub(options);
hub.connected(std::chrono::seconds(5));
auto messages = hub.getScope("chat").getVariable("messages");
// Message 1: Simple message (matches JS example)
messages.pub(nlohmann::json{
{"user", "Alice"},
{"message", "Hello World!"},
{"timestamp", std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count()}
});
std::cout << "Published message 1\n";
// Message 2: Another message
messages.pub(nlohmann::json{
{"user", "Bob"},
{"message", "How are you?"},
{"timestamp", std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count()}
});
std::cout << "Published message 2\n";
// Message 3: System message with extra fields
messages.pub(nlohmann::json{
{"user", "System"},
{"message", "Server restart in 5 minutes"},
{"type", "warning"},
{"priority", "high"},
{"timestamp", std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count()}
});
std::cout << "Published system message\n";
std::cout << "All messages published successfully!\n";
// Brief wait before disconnect (matching JS pattern)
std::this_thread::sleep_for(std::chrono::seconds(2));
hub.dispose();
std::cout << "Disconnected\n";
return 0;
}