nats_messaging 0.0.5 → 0.1.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 +18 -4
- metadata +2 -3
- data/lib/nats_messaging/nats_controller.rb +0 -55
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7fd232a55dacca429cfb77f5b13e6b10bb01ba24ac86bcfd46c37a0fde97ff7c
|
4
|
+
data.tar.gz: ae38d3e30cfe20b9cb9e2302021891234abead9b4c769d590ecf60052a74414c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1cee10a05ad976868e985b3a520c902c3b7f46dd9aff29a5a51152fea1f30604495f1bb945ce22adaf01d9f84a4a54f79d1b8b25e7259e5be05fe65c87125cbc
|
7
|
+
data.tar.gz: 81d636dca5d26286a80c51a8ad00b06cb30340c997a2a82472196f507defcc8aacdc8683bf98cbd178e569e584e20cb32ff9e1c5f34e37879b117946630da4a6
|
@@ -14,11 +14,14 @@ module NatsMessaging
|
|
14
14
|
begin
|
15
15
|
stream = @js.stream_info(stream_name)
|
16
16
|
Rails.logger.info "Stream '#{stream_name}' already exists"
|
17
|
+
puts "Stream #{stream_name} already exists"
|
17
18
|
rescue NATS::JetStream::Error::NotFound
|
18
19
|
@js.add_stream(name: stream_name, subjects: ["subject1", "subject2"])
|
19
20
|
Rails.logger.info "Stream '#{stream_name}' created"
|
21
|
+
puts "Stream #{stream_name} created"
|
20
22
|
rescue => e
|
21
23
|
Rails.logger.error "Failed to create stream: #{e.message}"
|
24
|
+
puts "Failed to create stream: #{e.message}"
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
@@ -37,16 +40,17 @@ module NatsMessaging
|
|
37
40
|
|
38
41
|
def subscribe_to_subject(subject, durable_name = "durable_name")
|
39
42
|
puts "suscribiendo a #{subject} keyword"
|
40
|
-
Rails.logger.info "keyword #{@nats.connected_server}"
|
41
43
|
@js.subscribe(subject, durable: durable_name) do |msg|
|
42
|
-
puts "mensaje recibido en #{subject}: #{msg.data}
|
43
|
-
Rails.logger.info "Message received on #{subject}: #{msg.data}"
|
44
|
+
puts "mensaje recibido en #{subject}: #{msg.data}"
|
45
|
+
Rails.logger.info "Message received on #{subject}: #{msg.data} keyword"
|
44
46
|
msg.ack
|
45
47
|
end
|
46
48
|
end
|
47
49
|
|
48
50
|
# Method to send a request
|
49
51
|
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}"
|
50
54
|
begin
|
51
55
|
response = @nats.request(subject, message, timeout: 5) # Timeout of 5 seconds
|
52
56
|
Rails.logger.info "Received reply: #{response.data}"
|
@@ -63,28 +67,38 @@ module NatsMessaging
|
|
63
67
|
# Method to listen and reply to messages
|
64
68
|
def listen_and_reply(subject, reply_message)
|
65
69
|
# Cancel active subscription if it already exists for this subject
|
70
|
+
Rails.logger.info "keyword listen and reply"
|
71
|
+
puts "keyword listen and reply"
|
66
72
|
if @subscriptions[subject]
|
67
73
|
@subscriptions[subject].unsubscribe
|
68
74
|
Rails.logger.info "Unsubscribed from #{subject}"
|
75
|
+
puts "Unsubscribed from #{subject}"
|
69
76
|
end
|
70
|
-
Rails.logger.info "keyword
|
77
|
+
Rails.logger.info "keyword #{@subscriptions[subject].inspect}"
|
78
|
+
puts "keyword #{@subscriptions[subject].inspect}"
|
71
79
|
|
72
80
|
begin
|
73
81
|
# Create a new subscription
|
74
82
|
Rails.logger.info "Suscribing a #{subject}"
|
83
|
+
puts "Suscribing a #{subject}"
|
75
84
|
subscription = @nats.subscribe(subject) do |msg|
|
76
85
|
Rails.logger.info "Request received on #{subject}: #{msg.data}"
|
86
|
+
puts "Request received on #{subject}: #{msg.data}"
|
77
87
|
msg.respond(reply_message)
|
78
88
|
Rails.logger.info "Replied to #{subject} with message: #{reply_message}"
|
89
|
+
puts "Replied to #{subject} with message: #{reply_message}"
|
79
90
|
end
|
80
91
|
rescue => e
|
81
92
|
Rails.logger.error "Error while replying: #{e.message}"
|
93
|
+
puts "Error while replying: #{e.message}"
|
82
94
|
end
|
83
95
|
|
84
96
|
# Store the new subscription in the hash
|
85
97
|
@subscriptions[subject] = subscription
|
86
98
|
Rails.logger.info "Stored subscription for #{subject}: #{@subscriptions[subject].inspect}"
|
99
|
+
puts "Stored subscription for #{subject}: #{@subscriptions[subject].inspect}"
|
87
100
|
Rails.logger.info "Listening on #{subject} with reply message: #{reply_message}"
|
101
|
+
puts "Listening on #{subject} with reply message: #{reply_message}"
|
88
102
|
end
|
89
103
|
end
|
90
104
|
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: 0.1.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-01-
|
11
|
+
date: 2025-01-28 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
|
@@ -17,7 +17,6 @@ extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- lib/nats_messaging.rb
|
20
|
-
- lib/nats_messaging/nats_controller.rb
|
21
20
|
- lib/nats_messaging/nats_service.rb
|
22
21
|
homepage: https://rubygems.org/gems/nats_messaging
|
23
22
|
licenses:
|
@@ -1,55 +0,0 @@
|
|
1
|
-
class NatsController < ApplicationController
|
2
|
-
|
3
|
-
def index
|
4
|
-
@durable = false # Default value for the toggle
|
5
|
-
end
|
6
|
-
|
7
|
-
def publish_message
|
8
|
-
subject = params[:subject]
|
9
|
-
message = params[:message]
|
10
|
-
|
11
|
-
nats_service = Rails.application.config.nats_service
|
12
|
-
nats_service.publish_message(subject, message)
|
13
|
-
|
14
|
-
flash[:notice] = "Message published to #{subject}."
|
15
|
-
redirect_to root_path
|
16
|
-
end
|
17
|
-
|
18
|
-
def subscribe_to_subject
|
19
|
-
subject = params[:subject]
|
20
|
-
|
21
|
-
nats_service = Rails.application.config.nats_service
|
22
|
-
nats_service.subscribe_to_subject(subject, "durable_#{subject}")
|
23
|
-
|
24
|
-
flash[:notice] = "Subscribed to #{subject}"
|
25
|
-
Rails.logger.info "Suscribed to #{subject}"
|
26
|
-
redirect_to root_path
|
27
|
-
end
|
28
|
-
|
29
|
-
def send_request
|
30
|
-
subject = params[:subject]
|
31
|
-
message = params[:message]
|
32
|
-
|
33
|
-
nats_service = NatsService.new
|
34
|
-
reply = nats_service.send_request(subject, message)
|
35
|
-
|
36
|
-
if reply
|
37
|
-
flash[:notice] = "Reply received: #{reply}"
|
38
|
-
else
|
39
|
-
flash[:alert] = "No reply received for subject: #{subject}"
|
40
|
-
end
|
41
|
-
|
42
|
-
redirect_to root_path
|
43
|
-
end
|
44
|
-
|
45
|
-
def listen_and_reply
|
46
|
-
subject = params[:subject]
|
47
|
-
reply_message = params[:reply_message]
|
48
|
-
|
49
|
-
nats_service = Rails.application.config.nats_service
|
50
|
-
nats_service.listen_and_reply(subject, reply_message)
|
51
|
-
|
52
|
-
flash[:notice] = "Listening for requests on #{subject} and replying with '#{reply_message}'"
|
53
|
-
redirect_to root_path
|
54
|
-
end
|
55
|
-
end
|