nats_messaging 0.1.4 → 0.1.6
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 +45 -26
- 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: 9a4f2955e4ab8e0529d03d2b2c9e38d938cb4e8a1046623c193cd9d7394c1cc1
|
4
|
+
data.tar.gz: 37ef0faba480951ad8f2eebd94dc0aef843ab97d3dadbe09bcd15931027ea040
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d4ccdbebca970edb5801a99a260f8d507c74c4bd2c1f857babe177dcd5fc11f484e8766b7be64d957df57087ef73f954d561866755638e8eae9fc82a6fdf62a
|
7
|
+
data.tar.gz: 10522507d0b80afc58c011d56bf84b4b42f6e4c679322baf41d0abc71a1852b141465d3fb12d33e62691d185ae9ebadea154e63096babf43f4fc572abbd4d0cc
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'nats/io/client'
|
2
|
+
require 'msgpack'
|
2
3
|
|
3
4
|
module NatsMessaging
|
4
5
|
class NatsService
|
@@ -13,7 +14,7 @@ module NatsMessaging
|
|
13
14
|
def create_stream(stream_name)
|
14
15
|
begin
|
15
16
|
stream = @js.stream_info(stream_name)
|
16
|
-
Rails.logger.info "Stream '#{stream_name}' already exists"
|
17
|
+
Rails.logger.info "Stream '#{stream_name}' already exists rails"
|
17
18
|
puts "Stream #{stream_name} already exists"
|
18
19
|
rescue NATS::JetStream::Error::NotFound
|
19
20
|
@js.add_stream(name: stream_name, subjects: ["subject1", "subject2"])
|
@@ -25,36 +26,44 @@ module NatsMessaging
|
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
28
|
-
# Publish a message to a subject
|
29
|
+
# Publish a message to a subject using MessagePack
|
29
30
|
def publish_message(subject, message)
|
30
|
-
Rails.logger.info "keyword #{@nats.connected_server}"
|
31
31
|
begin
|
32
|
-
|
32
|
+
packed_message = message.to_msgpack # Serializar a MessagePack
|
33
|
+
ack = @js.publish(subject, packed_message)
|
33
34
|
Rails.logger.info "Message sent to #{subject}: #{message}, ACK: #{ack.inspect}"
|
34
|
-
rescue JSON::ParserError => e
|
35
|
-
Rails.logger.error "Error parsing response: #{e.message}"
|
36
35
|
rescue => e
|
37
36
|
Rails.logger.error "Unexpected error: #{e.message}"
|
38
37
|
end
|
39
38
|
end
|
40
39
|
|
40
|
+
# Subscribe to a subject using MessagePack
|
41
41
|
def subscribe_to_subject(subject, durable_name = "durable_name")
|
42
|
-
puts "
|
42
|
+
puts "Subscribing to #{subject} keyword puuts"
|
43
43
|
@js.subscribe(subject, durable: durable_name) do |msg|
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
begin
|
45
|
+
unpacked_data = MessagePack.unpack(msg.data) # Deserializar el mensaje recibido
|
46
|
+
puts "Message received on #{subject}: #{unpacked_data}"
|
47
|
+
Rails.logger.info "Message received on #{subject}: #{unpacked_data} keyword"
|
48
|
+
msg.ack
|
49
|
+
unpacked_data
|
50
|
+
rescue => e
|
51
|
+
Rails.logger.error "Error while processing message: #{e.message}"
|
52
|
+
puts "Error while processing message: #{e.message}"
|
53
|
+
end
|
47
54
|
end
|
48
55
|
end
|
49
56
|
|
50
|
-
#
|
57
|
+
# Send a request to a subject and wait for response using MessagePack
|
51
58
|
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}"
|
59
|
+
Rails.logger.info "Sending request to #{subject} with message: #{message} rails"
|
60
|
+
puts "Sending request to #{subject} with message: #{message} puts"
|
54
61
|
begin
|
55
|
-
|
56
|
-
|
57
|
-
response.data
|
62
|
+
packed_message = message.to_msgpack # Serializar el mensaje
|
63
|
+
response = @nats.request(subject, packed_message, timeout: 5) # Timeout of 5 seconds
|
64
|
+
unpacked_response = MessagePack.unpack(response.data) # Deserializar respuesta
|
65
|
+
Rails.logger.info "Received reply: #{unpacked_response}"
|
66
|
+
unpacked_response
|
58
67
|
rescue NATS::IO::Timeout
|
59
68
|
Rails.logger.error "Request timed out for subject: #{subject}"
|
60
69
|
nil
|
@@ -64,11 +73,11 @@ module NatsMessaging
|
|
64
73
|
end
|
65
74
|
end
|
66
75
|
|
67
|
-
#
|
76
|
+
# Listen to a subject and reply with a message using MessagePack
|
68
77
|
def listen_and_reply(subject, reply_message)
|
69
78
|
# Cancel active subscription if it already exists for this subject
|
70
|
-
Rails.logger.info "keyword listen and reply"
|
71
|
-
puts "keyword listen and reply"
|
79
|
+
Rails.logger.info "keyword listen and reply rails"
|
80
|
+
puts "keyword listen and reply puts"
|
72
81
|
if @subscriptions[subject]
|
73
82
|
@subscriptions[subject].unsubscribe
|
74
83
|
Rails.logger.info "Unsubscribed from #{subject}"
|
@@ -82,13 +91,23 @@ module NatsMessaging
|
|
82
91
|
Rails.logger.info "Suscribing a #{subject}"
|
83
92
|
puts "Suscribing a #{subject}"
|
84
93
|
subscription = @nats.subscribe(subject) do |msg|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
94
|
+
begin
|
95
|
+
received_data = MessagePack.unpack(msg.data) # Deserializar mensaje recibido
|
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}"
|
92
111
|
end
|
93
112
|
end
|
94
113
|
rescue => e
|
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.6
|
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-30 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
|