|
| 1 | +require "nats" |
| 2 | +require "nats/services" |
| 3 | + |
| 4 | +# Get the `NATS_URL` from the environment or fallback to the default. This can |
| 5 | +# be a comma-separated string. We convert it to an `Array(URI)` to pass to the |
| 6 | +# NATS client. |
| 7 | +servers = ENV.fetch("NATS_URL", "nats://localhost:4222") |
| 8 | + .split(',') |
| 9 | + .map { |url| URI.parse(url) } |
| 10 | + |
| 11 | +# Create a client connection to an available NATS server. |
| 12 | +nats = NATS::Client.new(servers) |
| 13 | + |
| 14 | +# When the program exits, we close the NATS client which waits for any pending |
| 15 | +# messages (published or in a subscription) to be flushed. |
| 16 | +at_exit { nats.close } |
| 17 | + |
| 18 | +# ### Defining a service |
| 19 | +# |
| 20 | +# This will create a service definition. Service definitions are made up of |
| 21 | +# the service name (which can't have things like whitespace in it), a version, |
| 22 | +# and a description. Even with no running endpoints, this service is discoverable |
| 23 | +# via the service protocol and by service discovery tools like `nats service`. |
| 24 | +# All of the default background handlers for discovery, PING, and stats are |
| 25 | +# started at this point. |
| 26 | +service = nats.services.add "minmax", |
| 27 | + version: "0.0.1", |
| 28 | + description: "Returns the min/max number in a request" |
| 29 | + |
| 30 | +puts "Created service: #{service.name} (#{service.id})" |
| 31 | + |
| 32 | +# ### Adding endpoints |
| 33 | +# |
| 34 | +# Groups serve as namespaces and are used as a subject prefix when endpoints |
| 35 | +# don't supply fixed subjects. In this case, all endpoints will be listening |
| 36 | +# on a subject that starts with `minmax.` |
| 37 | +service.add_group "minmax" do |root| |
| 38 | + # Each endpoint represents a subscription. The supplied handlers will respond |
| 39 | + # to `minmax.min` and `minmax.max`, respectively. |
| 40 | + |
| 41 | + # Add a `min` endpoint to the service, which returns the minimum |
| 42 | + # value from a JSON-serialized list of integer values. |
| 43 | + root.add_endpoint "min" do |request| |
| 44 | + min = Array(Int64).from_json(request.data_string).min |
| 45 | + |
| 46 | + nats.reply request, min.to_json |
| 47 | + end |
| 48 | + |
| 49 | + # Add a `max` endpoint to the service, which returns the maximum |
| 50 | + # value from a JSON-serialized list of integer values. |
| 51 | + root.add_endpoint "max" do |request| |
| 52 | + max = Array(Int64).from_json(request.data_string).max |
| 53 | + |
| 54 | + nats.reply request, max.to_json |
| 55 | + end |
| 56 | +end |
| 57 | + |
| 58 | +# ### Sending a request |
| 59 | +# |
| 60 | +# Now we create a list of integer values to send to our `minmax.min` and |
| 61 | +# `minmax.max` endpoints. |
| 62 | +request_data = [-1, 2, 100, -2000] |
| 63 | + |
| 64 | +# Send a request to the `minmax.min` endpoint containing the array above. Note |
| 65 | +# that this is just a regular NATS request. |
| 66 | +if response = nats.request("minmax.min", request_data.to_json, timeout: 2.seconds) |
| 67 | + puts "Requested min value, got #{response.data_string.to_i}" |
| 68 | +end |
| 69 | + |
| 70 | +# Now we do the same thing with our `minmax.max` endpoint. |
| 71 | +if response = nats.request("minmax.max", request_data.to_json, timeout: 2.seconds) |
| 72 | + puts "Requested max value, got #{response.data_string.to_i}" |
| 73 | +end |
0 commit comments