nats_messaging 0.0.1 → 0.0.3
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 +87 -0
- data/lib/nats_messaging.rb +0 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 05ebb509e6c23ab771e8f23e3b7e8fa711b061c328d879021ee33eb854afb261
|
4
|
+
data.tar.gz: f3e60a1c2e55aa2c7e424cad7ff2fa8befc58a472d930013587b33d74f3ca8ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: badf0321fc6c4a058373d7e6a3a5ed25b4419e376dd460b4b6d755c60bbf18bb6caa6a88b3c99ebd5390c0760bfa34ecce1195d89b60c8802a088b14760e1af4
|
7
|
+
data.tar.gz: 9f0d622bcc445b559f87b61f3079c83890a3a23dd2392cd9541fe3b962ce04fcc509559a3b012c84efc4b9a5a5af6ddf853a0daaf1e15191eb1c698b7426c739
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'nats/io/client'
|
2
|
+
|
3
|
+
module NatsMessaging
|
4
|
+
class NatsService
|
5
|
+
def initialize
|
6
|
+
@nats = NATS.connect("nats://localhost:4222")
|
7
|
+
@js = @nats.jetstream
|
8
|
+
create_stream("default_stream")
|
9
|
+
@subscriptions = {} # Store active subscriptions
|
10
|
+
end
|
11
|
+
|
12
|
+
# Create a stream
|
13
|
+
def create_stream(stream_name)
|
14
|
+
begin
|
15
|
+
stream = @js.stream_info(stream_name)
|
16
|
+
Rails.logger.info "Stream '#{stream_name}' already exists"
|
17
|
+
rescue NATS::JetStream::Error::NotFound
|
18
|
+
@js.add_stream(name: stream_name, subjects: ["subject1", "subject2"])
|
19
|
+
Rails.logger.info "Stream '#{stream_name}' created"
|
20
|
+
rescue => e
|
21
|
+
Rails.logger.error "Failed to create stream: #{e.message}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Publish a message to a subject
|
26
|
+
def publish_message(subject, message)
|
27
|
+
Rails.logger.info "keyword #{@nats.connected_server}"
|
28
|
+
begin
|
29
|
+
ack = @js.publish(subject, message)
|
30
|
+
Rails.logger.info "Message sent to #{subject}: #{message}, ACK: #{ack.inspect}"
|
31
|
+
rescue JSON::ParserError => e
|
32
|
+
Rails.logger.error "Error parsing response: #{e.message}"
|
33
|
+
rescue => e
|
34
|
+
Rails.logger.error "Unexpected error: #{e.message}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def subscribe_to_subject(subject, durable_name = "durable_name")
|
39
|
+
Rails.logger.info "keyword #{@nats.connected_server}"
|
40
|
+
@js.subscribe(subject, durable: durable_name) do |msg|
|
41
|
+
Rails.logger.info "Message received on #{subject}: #{msg.data}"
|
42
|
+
msg.ack
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Method to send a request
|
47
|
+
def send_request(subject, message)
|
48
|
+
begin
|
49
|
+
response = @nats.request(subject, message, timeout: 5) # Timeout of 5 seconds
|
50
|
+
Rails.logger.info "Received reply: #{response.data}"
|
51
|
+
response.data
|
52
|
+
rescue NATS::IO::Timeout
|
53
|
+
Rails.logger.error "Request timed out for subject: #{subject}"
|
54
|
+
nil
|
55
|
+
rescue => e
|
56
|
+
Rails.logger.error "Unexpected error: #{e.message}"
|
57
|
+
nil
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Method to listen and reply to messages
|
62
|
+
def listen_and_reply(subject, reply_message)
|
63
|
+
# Cancel active subscription if it already exists for this subject
|
64
|
+
if @subscriptions[subject]
|
65
|
+
@subscriptions[subject].unsubscribe
|
66
|
+
Rails.logger.info "Unsubscribed from #{subject}"
|
67
|
+
end
|
68
|
+
Rails.logger.info "keyword ecm #{@subscriptions[subject].inspect}"
|
69
|
+
|
70
|
+
begin
|
71
|
+
# Create a new subscription
|
72
|
+
Rails.logger.info "Suscribing a #{subject}"
|
73
|
+
subscription = @nats.subscribe(subject) do |msg|
|
74
|
+
Rails.logger.info "Request received on #{subject}: #{msg.data}"
|
75
|
+
msg.respond(reply_message)
|
76
|
+
Rails.logger.info "Replied to #{subject} with message: #{reply_message}"
|
77
|
+
end
|
78
|
+
rescue => e
|
79
|
+
Rails.logger.error "Error while replying: #{e.message}"
|
80
|
+
end
|
81
|
+
|
82
|
+
# Store the new subscription in the hash
|
83
|
+
@subscriptions[subject] = subscription
|
84
|
+
Rails.logger.info "Stored subscription for #{subject}: #{@subscriptions[subject].inspect}"
|
85
|
+
Rails.logger.info "Listening on #{subject} with reply message: #{reply_message}"
|
86
|
+
end
|
87
|
+
end
|
data/lib/nats_messaging.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nats_messaging
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bea Graboloza
|
@@ -18,6 +18,7 @@ extra_rdoc_files: []
|
|
18
18
|
files:
|
19
19
|
- lib/nats_messaging.rb
|
20
20
|
- lib/nats_messaging/nats_controller.rb
|
21
|
+
- lib/nats_messaging/nats_service.rb
|
21
22
|
homepage: https://rubygems.org/gems/nats_messaging
|
22
23
|
licenses:
|
23
24
|
- MIT
|