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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a176a75f645e513add22d8a4c652212e46bfa1b8ae82f414e02c29bbd4a7bd09
4
- data.tar.gz: b665a4fa91e3793fb6eb2a93e554805ede8c9b0c7d5e5b81d95b15b00650d295
3
+ metadata.gz: a3e8eef7b8b10d8b83e5ce8a0236def7eb58d9d99325e2c2d64871554c470c0c
4
+ data.tar.gz: 443547eff24fbbf7381e0bc9234bc56531e97bc10052b227d3372cc0f4ae7825
5
5
  SHA512:
6
- metadata.gz: 3b9fd0b44e60e9b6625ffeebd76198824d8ab783c0a6b4960b9c4c8cca05aff660fcf6eb6a4f3283867c1bf7fa863cfa10a3b0ef9e17fbbc02e4d31ccb2c7711
7
- data.tar.gz: f738b9bdc0ae7f1dbd73500b165e1bfb202c37a0832a1dd3869104a1f5c518a879d015b1e8d0cd50a909e332dada853535d85a19f62e88a2996edc74d53d9729
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
- Rails.logger.info "Stream '#{stream_name}' already exists"
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
- Rails.logger.info "Stream '#{stream_name}' created"
21
- puts "Stream #{stream_name} created"
20
+ puts "NATS: Stream #{stream_name} created"
22
21
  rescue => e
23
- Rails.logger.error "Failed to create stream: #{e.message}"
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
- ack = @js.publish(subject, message)
33
- 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}"
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
- Rails.logger.error "Unexpected error: #{e.message}"
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 "suscribiendo a #{subject} keyword"
39
+ puts "NATS: Subscribing to #{subject}"
43
40
  @js.subscribe(subject, durable: durable_name) do |msg|
44
- puts "mensaje recibido en #{subject}: #{msg.data}"
45
- Rails.logger.info "Message received on #{subject}: #{msg.data} keyword"
46
- msg.ack
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
- # Method to send a request
52
+ # Send a request to a subject and wait for response using MessagePack
51
53
  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}"
54
+ puts "NATS: Sending request to #{subject} with message: #{message}"
54
55
  begin
55
- response = @nats.request(subject, message, timeout: 5) # Timeout of 5 seconds
56
- Rails.logger.info "Received reply: #{response.data}"
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
- Rails.logger.error "Request timed out for subject: #{subject}"
62
+ puts "NATS: Request timed out for subject: #{subject}"
60
63
  nil
61
64
  rescue => e
62
- Rails.logger.error "Unexpected error: #{e.message}"
65
+ puts "NATS: Unexpected error: #{e.message}"
63
66
  nil
64
67
  end
65
68
  end
66
69
 
67
- # Method to listen and reply to messages
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
- Rails.logger.info "Suscribing a #{subject}"
83
- puts "Suscribing a #{subject}"
78
+ puts "NATS: Suscribing to #{subject}"
84
79
  subscription = @nats.subscribe(subject) do |msg|
85
- # Asegurar que msg.reply existe antes de responder
86
- if msg.reply
87
- @nats.publish(msg.reply, reply_message)
88
- Rails.logger.info "Replied to #{subject} with message: #{reply_message} new"
89
- puts "Replied to #{subject} with message: #{reply_message} new"
90
- else
91
- Rails.logger.error "No reply subject for #{subject}, cannot respond"
92
- puts "No reply subject for #{subject}, cannot respond"
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
- Rails.logger.error "Error while replying: #{e.message}"
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
- Rails.logger.info "Stored subscription for #{subject}: #{@subscriptions[subject].inspect}"
103
- puts "Stored subscription for #{subject}: #{@subscriptions[subject].inspect}"
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.5
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-29 00:00:00.000000000 Z
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