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