nats_messaging 0.1.6 → 1.0.0
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 +82 -68
- data/lib/nats_messaging.rb +2 -2
- 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: e63689a8e27b2c36f01a7ecb7fb5f46fe61ca174666a98af7b6ce7ca594eb952
|
4
|
+
data.tar.gz: 44dbfbea1f01c068ff64d26bf0741d926271c03f5f057d2f54c62eda5d9dd78f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 03fceb9b443cc5e94086757a2c4e6b53ff68aaf9aea3ce1f207e91553b85a24108573a1178be7b7e708cd872009014bdccc0f2b107acbf0013aad0fe56fecd62
|
7
|
+
data.tar.gz: e8bb0c6130aac20266b96dbcde5d01ee7f08ac10864addabedc84c61d713873369d7ef12b7d391d8d64a7f9dab95ee8df1a67753ceccb9220df0c50afbf195f6
|
@@ -3,72 +3,95 @@ require 'msgpack'
|
|
3
3
|
|
4
4
|
module NatsMessaging
|
5
5
|
class NatsService
|
6
|
-
def initialize
|
7
|
-
|
8
|
-
@
|
9
|
-
|
6
|
+
def initialize(nats_url, stream_name: nil, subjects: [])
|
7
|
+
# para correr en local usar "nats://localhost:4222" como nats_url
|
8
|
+
@nats = NATS.connect(nats_url)
|
9
|
+
if stream_name
|
10
|
+
@js = @nats.jetstream
|
11
|
+
create_stream(stream_name, subjects)
|
12
|
+
end
|
10
13
|
@subscriptions = {} # Store active subscriptions
|
11
14
|
end
|
12
15
|
|
13
16
|
# Create a stream
|
14
|
-
def create_stream(stream_name)
|
17
|
+
def create_stream(stream_name, subjects)
|
15
18
|
begin
|
16
19
|
stream = @js.stream_info(stream_name)
|
17
|
-
|
18
|
-
puts "Stream #{stream_name} already exists"
|
20
|
+
puts "NATS: Stream #{stream_name} already exists"
|
19
21
|
rescue NATS::JetStream::Error::NotFound
|
20
|
-
@js.add_stream(name: stream_name, subjects:
|
21
|
-
|
22
|
-
puts "Stream #{stream_name} created"
|
22
|
+
@js.add_stream(name: stream_name, subjects: subjects)
|
23
|
+
puts "NATS: Stream #{stream_name} created"
|
23
24
|
rescue => e
|
24
|
-
|
25
|
-
puts "Failed to create stream: #{e.message}"
|
25
|
+
puts "NATS: Failed to create stream: #{e.message}"
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
# Publish a message to a subject using MessagePack
|
30
30
|
def publish_message(subject, message)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
31
|
+
packed_message = message.to_msgpack # Serializar a MessagePack
|
32
|
+
|
33
|
+
if @js
|
34
|
+
begin
|
35
|
+
ack = @js.publish(subject, packed_message)
|
36
|
+
puts "NATS: Message sent to #{subject}: #{message}, ACK: #{ack.inspect}"
|
37
|
+
rescue NATS::JetStream::Error::NotFound
|
38
|
+
@nats.publish(subject, packed_message)
|
39
|
+
puts "NATS: Stream not found, falling back to regular NATS publish"
|
40
|
+
rescue => e
|
41
|
+
puts "NATS: Unexpected error: #{e.message}"
|
42
|
+
end
|
43
|
+
else
|
44
|
+
begin
|
45
|
+
@nats.publish(subject, packed_message)
|
46
|
+
puts "NATS: Message sent to #{subject}: #{message}, ACK: #{ack.inspect}"
|
47
|
+
rescue => e
|
48
|
+
puts "NATS: Unexpected error: #{e.message}"
|
37
49
|
end
|
38
50
|
end
|
39
51
|
|
40
52
|
# Subscribe to a subject using MessagePack
|
41
53
|
def subscribe_to_subject(subject, durable_name = "durable_name")
|
42
|
-
puts "Subscribing to #{subject}
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
+
puts "NATS: Subscribing to #{subject}"
|
55
|
+
|
56
|
+
if @js
|
57
|
+
subscription = @js.subscribe(subject, durable: durable_name) do |msg|
|
58
|
+
process_received_message(msg, subject)
|
59
|
+
end
|
60
|
+
else
|
61
|
+
subscription = @nats.subscribe(subject) do |msg|
|
62
|
+
process_received_message(msg, subject)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
@subscriptions[subject] = subscription
|
67
|
+
end
|
68
|
+
|
69
|
+
# Process received message
|
70
|
+
def process_received_message(msg, subject)
|
71
|
+
begin
|
72
|
+
unpacked_data = MessagePack.unpack(msg.data)
|
73
|
+
puts "NATS: Message received on #{subject}: #{unpacked_data}"
|
74
|
+
msg.ack if msg.respond_to?(:ack)
|
75
|
+
unpacked_data
|
76
|
+
rescue => e
|
77
|
+
puts "NATS: Error while processing message: #{e.message}"
|
54
78
|
end
|
55
79
|
end
|
56
80
|
|
57
81
|
# Send a request to a subject and wait for response using MessagePack
|
58
82
|
def send_request(subject, message)
|
59
|
-
|
60
|
-
puts "Sending request to #{subject} with message: #{message} puts"
|
83
|
+
puts "NATS: Sending request to #{subject} with message: #{message}"
|
61
84
|
begin
|
62
85
|
packed_message = message.to_msgpack # Serializar el mensaje
|
63
86
|
response = @nats.request(subject, packed_message, timeout: 5) # Timeout of 5 seconds
|
64
87
|
unpacked_response = MessagePack.unpack(response.data) # Deserializar respuesta
|
65
|
-
|
88
|
+
puts "NATS: Received reply: #{unpacked_response}"
|
66
89
|
unpacked_response
|
67
90
|
rescue NATS::IO::Timeout
|
68
|
-
|
91
|
+
puts "NATS: Request timed out for subject: #{subject}"
|
69
92
|
nil
|
70
93
|
rescue => e
|
71
|
-
|
94
|
+
puts "NATS: Unexpected error: #{e.message}"
|
72
95
|
nil
|
73
96
|
end
|
74
97
|
end
|
@@ -76,51 +99,42 @@ module NatsMessaging
|
|
76
99
|
# Listen to a subject and reply with a message using MessagePack
|
77
100
|
def listen_and_reply(subject, reply_message)
|
78
101
|
# Cancel active subscription if it already exists for this subject
|
79
|
-
Rails.logger.info "keyword listen and reply rails"
|
80
|
-
puts "keyword listen and reply puts"
|
81
102
|
if @subscriptions[subject]
|
82
103
|
@subscriptions[subject].unsubscribe
|
83
|
-
Rails.logger.info "Unsubscribed from #{subject}"
|
84
|
-
puts "Unsubscribed from #{subject}"
|
85
104
|
end
|
86
|
-
Rails.logger.info "keyword #{@subscriptions[subject].inspect}"
|
87
|
-
puts "keyword #{@subscriptions[subject].inspect}"
|
88
|
-
|
89
105
|
begin
|
90
106
|
# Create a new subscription
|
91
|
-
|
92
|
-
puts "Suscribing a #{subject}"
|
107
|
+
puts "NATS: Suscribing to #{subject}"
|
93
108
|
subscription = @nats.subscribe(subject) do |msg|
|
94
|
-
|
95
|
-
|
96
|
-
Rails.logger.info "Received request on #{subject}: #{received_data}"
|
97
|
-
puts "Received request on #{subject}: #{received_data}"
|
98
|
-
packed_reply = reply_message.to_msgpack # Serializar respuesta
|
99
|
-
# Asegurar que msg.reply existe antes de responder
|
100
|
-
if msg.reply
|
101
|
-
@nats.publish(msg.reply, packed_reply)
|
102
|
-
Rails.logger.info "Replied to #{subject} with message: #{reply_message} new"
|
103
|
-
puts "Replied to #{subject} with message: #{reply_message} new"
|
104
|
-
else
|
105
|
-
Rails.logger.error "No reply subject for #{subject}, cannot respond"
|
106
|
-
puts "No reply subject for #{subject}, cannot respond"
|
107
|
-
end
|
108
|
-
rescue => e
|
109
|
-
Rails.logger.error "Error while processing message: #{e.message}"
|
110
|
-
puts "Error while processing message: #{e.message}"
|
111
|
-
end
|
109
|
+
process_reply(msg, subject, reply_message)
|
110
|
+
|
112
111
|
end
|
113
112
|
rescue => e
|
114
|
-
|
115
|
-
puts "Error while replying: #{e.message}"
|
113
|
+
puts "NATS: Error while replying: #{e.message}"
|
116
114
|
end
|
117
115
|
|
118
116
|
# Store the new subscription in the hash
|
119
117
|
@subscriptions[subject] = subscription
|
120
|
-
|
121
|
-
puts "
|
122
|
-
|
123
|
-
|
118
|
+
puts "NATS: Stored subscription for #{subject}: #{@subscriptions[subject].inspect}"
|
119
|
+
puts "NATS: Listening on #{subject} with reply message: #{reply_message}"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# Process and reply a message received
|
124
|
+
def process_reply(msg, subject, reply_message)
|
125
|
+
begin
|
126
|
+
unpacked_data = MessagePack.unpack(msg.data) # Deserializar mensaje recibido
|
127
|
+
puts "NATS: Received request on #{subject}: #{received_data}"
|
128
|
+
packed_reply = reply_message.to_msgpack # Serializar respuesta
|
129
|
+
# Asegurar que msg.reply existe antes de responder
|
130
|
+
if msg.reply
|
131
|
+
@nats.publish(msg.reply, packed_reply)
|
132
|
+
puts "NATS: Replied to #{subject} with message: #{reply_message} new"
|
133
|
+
else
|
134
|
+
puts "NATS: No reply subject for #{subject}, cannot respond"
|
135
|
+
end
|
136
|
+
rescue => e
|
137
|
+
puts "NATS: Error while processing message: #{e.message}"
|
124
138
|
end
|
125
139
|
end
|
126
140
|
end
|
data/lib/nats_messaging.rb
CHANGED
@@ -2,7 +2,7 @@ require 'nats_messaging/nats_service'
|
|
2
2
|
|
3
3
|
module NatsMessaging
|
4
4
|
# Main entry point to expose the functionalities
|
5
|
-
def self.service
|
6
|
-
@service ||= NatsService.new
|
5
|
+
def self.service(nats_url, stream_name: nil, subjects: [])
|
6
|
+
@service ||= NatsService.new(nats_url, stream_name: stream_name, subjects: subjects)
|
7
7
|
end
|
8
8
|
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.
|
4
|
+
version: 1.0.0
|
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-
|
11
|
+
date: 2025-02-11 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
|