nats_messaging 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/nats_messaging/nats_service.rb +20 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4dfafceac9fc28598652a678231f547085a95f7ed8864794f38fa6cd9c5cdae0
|
4
|
+
data.tar.gz: 47b5c18dff08155575bd946209f475e1f7ac50b77a29371d841fbd46940207c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9650332cfc62a4d62891f852ce98b3b1aa71b13cb12ec742aa201885cf4905d83003add5b25d14946dcb5edd81348ddca06a7daf8d785c0f57b0cc404c12d76
|
7
|
+
data.tar.gz: 00b8cfc2b33d8e617f99a9c56bcb228d88c7d0ccd2179dccfa1f75ace0991344cbd44f7bee07f97e4081f79549f1023caf94dadf223daacdb35cfa3455c14183
|
@@ -14,11 +14,14 @@ module NatsMessaging
|
|
14
14
|
begin
|
15
15
|
stream = @js.stream_info(stream_name)
|
16
16
|
Rails.logger.info "Stream '#{stream_name}' already exists"
|
17
|
+
puts "Stream #{stream_name} already exists"
|
17
18
|
rescue NATS::JetStream::Error::NotFound
|
18
19
|
@js.add_stream(name: stream_name, subjects: ["subject1", "subject2"])
|
19
20
|
Rails.logger.info "Stream '#{stream_name}' created"
|
21
|
+
puts "Stream #{stream_name} created"
|
20
22
|
rescue => e
|
21
23
|
Rails.logger.error "Failed to create stream: #{e.message}"
|
24
|
+
puts "Failed to create stream: #{e.message}"
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
@@ -37,20 +40,21 @@ module NatsMessaging
|
|
37
40
|
|
38
41
|
def subscribe_to_subject(subject, durable_name = "durable_name")
|
39
42
|
puts "suscribiendo a #{subject} keyword"
|
40
|
-
Rails.logger.info "keyword #{@nats.connected_server}"
|
41
43
|
@js.subscribe(subject, durable: durable_name) do |msg|
|
42
|
-
puts "mensaje recibido en #{subject}: #{msg.data}
|
43
|
-
Rails.logger.info "Message received on #{subject}: #{msg.data}"
|
44
|
+
puts "mensaje recibido en #{subject}: #{msg.data}"
|
45
|
+
Rails.logger.info "Message received on #{subject}: #{msg.data} keyword"
|
44
46
|
msg.ack
|
45
47
|
end
|
46
48
|
end
|
47
49
|
|
48
50
|
# Method to send a request
|
49
51
|
def send_request(subject, message)
|
52
|
+
Rails.logger.info "Sending request to #{subject} with message: #{message}"
|
53
|
+
puts "Sending request to #{subject} with message: #{message}"
|
50
54
|
begin
|
51
55
|
response = @nats.request(subject, message, timeout: 5) # Timeout of 5 seconds
|
52
|
-
Rails.logger.info "Received reply: #{response.
|
53
|
-
response.
|
56
|
+
Rails.logger.info "Received reply: #{response.reply}"
|
57
|
+
response.reply
|
54
58
|
rescue NATS::IO::Timeout
|
55
59
|
Rails.logger.error "Request timed out for subject: #{subject}"
|
56
60
|
nil
|
@@ -63,28 +67,38 @@ module NatsMessaging
|
|
63
67
|
# Method to listen and reply to messages
|
64
68
|
def listen_and_reply(subject, reply_message)
|
65
69
|
# Cancel active subscription if it already exists for this subject
|
70
|
+
Rails.logger.info "keyword listen and reply"
|
71
|
+
puts "keyword listen and reply"
|
66
72
|
if @subscriptions[subject]
|
67
73
|
@subscriptions[subject].unsubscribe
|
68
74
|
Rails.logger.info "Unsubscribed from #{subject}"
|
75
|
+
puts "Unsubscribed from #{subject}"
|
69
76
|
end
|
70
|
-
Rails.logger.info "keyword
|
77
|
+
Rails.logger.info "keyword #{@subscriptions[subject].inspect}"
|
78
|
+
puts "keyword #{@subscriptions[subject].inspect}"
|
71
79
|
|
72
80
|
begin
|
73
81
|
# Create a new subscription
|
74
82
|
Rails.logger.info "Suscribing a #{subject}"
|
83
|
+
puts "Suscribing a #{subject}"
|
75
84
|
subscription = @nats.subscribe(subject) do |msg|
|
76
85
|
Rails.logger.info "Request received on #{subject}: #{msg.data}"
|
86
|
+
puts "Request received on #{subject}: #{msg.data}"
|
77
87
|
msg.respond(reply_message)
|
78
88
|
Rails.logger.info "Replied to #{subject} with message: #{reply_message}"
|
89
|
+
puts "Replied to #{subject} with message: #{reply_message}"
|
79
90
|
end
|
80
91
|
rescue => e
|
81
92
|
Rails.logger.error "Error while replying: #{e.message}"
|
93
|
+
puts "Error while replying: #{e.message}"
|
82
94
|
end
|
83
95
|
|
84
96
|
# Store the new subscription in the hash
|
85
97
|
@subscriptions[subject] = subscription
|
86
98
|
Rails.logger.info "Stored subscription for #{subject}: #{@subscriptions[subject].inspect}"
|
99
|
+
puts "Stored subscription for #{subject}: #{@subscriptions[subject].inspect}"
|
87
100
|
Rails.logger.info "Listening on #{subject} with reply message: #{reply_message}"
|
101
|
+
puts "Listening on #{subject} with reply message: #{reply_message}"
|
88
102
|
end
|
89
103
|
end
|
90
104
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nats_messaging
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bea Graboloza
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-01-
|
11
|
+
date: 2025-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A service to send and receive messages on a Rails application using NATS
|
14
14
|
email: beatriz.graboloza@bpo-advisors.net
|