nats_messaging 0.1.7 → 1.0.1
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 +71 -35
- 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: 362ffb41d76609d059fbead81c5989ba38eeb75c2a74495128c23adebe22c44b
|
4
|
+
data.tar.gz: 74c67fec24a28c1e5f8e3083a823c0623a9f70201402df543514356df143a01d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28b2311dfd747fe27c7f9688f4c5c89c0aca3634eeca153d0b050fc3cf96e3578204d4c751ea3b0e12e8b29bc6f086fc6aab53fc69d1eb2d50bb3163360aa89c
|
7
|
+
data.tar.gz: 94cd5da40b5ee9bb4bb78fd44c8c43225fbee8edd8b0f25824616d1c4ba37ae1773cbfa68f5cb21e9d58825307e66fbc9e8e0c193468c72bb5a6291503432551
|
@@ -3,20 +3,23 @@ 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
20
|
puts "NATS: Stream #{stream_name} already exists"
|
18
21
|
rescue NATS::JetStream::Error::NotFound
|
19
|
-
@js.add_stream(name: stream_name, subjects:
|
22
|
+
@js.add_stream(name: stream_name, subjects: subjects)
|
20
23
|
puts "NATS: Stream #{stream_name} created"
|
21
24
|
rescue => e
|
22
25
|
puts "NATS: Failed to create stream: #{e.message}"
|
@@ -25,27 +28,54 @@ module NatsMessaging
|
|
25
28
|
|
26
29
|
# Publish a message to a subject using MessagePack
|
27
30
|
def publish_message(subject, message)
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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}"
|
49
|
+
end
|
34
50
|
end
|
35
51
|
end
|
36
52
|
|
37
53
|
# Subscribe to a subject using MessagePack
|
38
54
|
def subscribe_to_subject(subject, durable_name = "durable_name")
|
39
55
|
puts "NATS: Subscribing to #{subject}"
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
56
|
+
|
57
|
+
if @js
|
58
|
+
subscription = @js.subscribe(subject, durable: durable_name) do |msg|
|
59
|
+
process_received_message(msg, subject)
|
60
|
+
end
|
61
|
+
else
|
62
|
+
subscription = @nats.subscribe(subject) do |msg|
|
63
|
+
process_received_message(msg, subject)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
@subscriptions[subject] = subscription
|
68
|
+
end
|
69
|
+
|
70
|
+
# Process received message
|
71
|
+
def process_received_message(msg, subject)
|
72
|
+
begin
|
73
|
+
unpacked_data = MessagePack.unpack(msg.data)
|
74
|
+
puts "NATS: Message received on #{subject}: #{unpacked_data}"
|
75
|
+
msg.ack if msg.respond_to?(:ack)
|
76
|
+
unpacked_data
|
77
|
+
rescue => e
|
78
|
+
puts "NATS: Error while processing message: #{e.message}"
|
49
79
|
end
|
50
80
|
end
|
51
81
|
|
@@ -77,20 +107,8 @@ module NatsMessaging
|
|
77
107
|
# Create a new subscription
|
78
108
|
puts "NATS: Suscribing to #{subject}"
|
79
109
|
subscription = @nats.subscribe(subject) do |msg|
|
80
|
-
|
81
|
-
|
82
|
-
puts "NATS: Received request on #{subject}: #{received_data}"
|
83
|
-
packed_reply = reply_message.to_msgpack # Serializar respuesta
|
84
|
-
# Asegurar que msg.reply existe antes de responder
|
85
|
-
if msg.reply
|
86
|
-
@nats.publish(msg.reply, packed_reply)
|
87
|
-
puts "NATS: Replied to #{subject} with message: #{reply_message} new"
|
88
|
-
else
|
89
|
-
puts "NATS: No reply subject for #{subject}, cannot respond"
|
90
|
-
end
|
91
|
-
rescue => e
|
92
|
-
puts "NATS: Error while processing message: #{e.message}"
|
93
|
-
end
|
110
|
+
process_reply(msg, subject, reply_message)
|
111
|
+
|
94
112
|
end
|
95
113
|
rescue => e
|
96
114
|
puts "NATS: Error while replying: #{e.message}"
|
@@ -102,4 +120,22 @@ module NatsMessaging
|
|
102
120
|
puts "NATS: Listening on #{subject} with reply message: #{reply_message}"
|
103
121
|
end
|
104
122
|
end
|
123
|
+
|
124
|
+
# Process and reply a message received
|
125
|
+
def process_reply(msg, subject, reply_message)
|
126
|
+
begin
|
127
|
+
unpacked_data = MessagePack.unpack(msg.data) # Deserializar mensaje recibido
|
128
|
+
puts "NATS: Received request on #{subject}: #{received_data}"
|
129
|
+
packed_reply = reply_message.to_msgpack # Serializar respuesta
|
130
|
+
# Asegurar que msg.reply existe antes de responder
|
131
|
+
if msg.reply
|
132
|
+
@nats.publish(msg.reply, packed_reply)
|
133
|
+
puts "NATS: Replied to #{subject} with message: #{reply_message} new"
|
134
|
+
else
|
135
|
+
puts "NATS: No reply subject for #{subject}, cannot respond"
|
136
|
+
end
|
137
|
+
rescue => e
|
138
|
+
puts "NATS: Error while processing message: #{e.message}"
|
139
|
+
end
|
140
|
+
end
|
105
141
|
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.1
|
4
|
+
version: 1.0.1
|
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
|