diff --git a/README.md b/README.md index 8025697..3dab729 100644 --- a/README.md +++ b/README.md @@ -20,21 +20,29 @@ Simple NATS client for the [Crystal](https://crystal-lang.org) programming langu ## Usage ```crystal -require "nats" +require "nats/client" -nc = NATS::Connection.new("demo.nats.io") -nc.subscribe("foo") { |msg| puts "Received '#{msg}'"} -nc.publish("foo", "Hello!") +client = NATS::Client.new("nats://localhost:4222") +client.connect -sub = nc.subscribe("req") do |msg| - msg.respond("ANSWER is 42") +client.subscribe("foo") do |msg| + puts "Received message on subject #{msg.subject}: #{msg.payload}" end -answer = nc.request("req", "Help!") -puts "Received a response '#{answer}'!" +client.publish("foo", "Hello, NATS!") -sub.close -nc.close +client.subscribe("bar") do |msg| + msg.reply "Received your message on bar: #{msg.payload}" +end + +response = client.request("bar", "Hello, bar!") +puts "Received response: #{response.payload}" + +client.request("bar") do |msg| + puts "Recieved async response: #{msg.payload}" +end + +client.close ``` ## License diff --git a/TODO b/TODO index d6e8e0a..e3be25c 100644 --- a/TODO +++ b/TODO @@ -1,8 +1,5 @@ -1. auto-unsub, max messages -2. Multiple URLs/Servers -3. Reconnect logic -4. Async INFO processing -5. Multiple servers, state logic CONNECTED/RECONNECTING/DISCONNECTED/CLOSED -6. msgs/bytes stats for connections and subscriptions -7. async request? -8. NATS 2.0 security/auth +1. Reconnect logic +2. Handle multiple servers +3. State change callbacks +4. auto-unsub, max messages (https://docs.nats.io/using-nats/developer/receiving/unsub_after) +5. msgs/bytes stats for connections and subscriptions diff --git a/bin/nats-pub.cr b/bin/nats-pub.cr deleted file mode 100644 index 3263d87..0000000 --- a/bin/nats-pub.cr +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright 2019 The NATS Authors -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require "option_parser" -require "../src/nats/connection" - -USAGE = "Usage: nats-pub [-s server] " - -def usage - STDERR.puts USAGE; exit(1) -end - -server = NATS::Connection::DEFAULT_URI -args = ARGV.dup - -OptionParser.parse(args) do |parser| - parser.banner = USAGE - parser.on("-s SERVER", "--server=SERVER", "NATS Server to connect") { |url| server = url } - parser.on("-h", "--help", "Show this help") { usage } - parser.invalid_option do |flag| - STDERR.puts "ERROR: #{flag} is not a valid option." - usage - end -end - -usage unless args.size == 2 -subject, msg = args - -nc = NATS::Connection.new(server) -at_exit { nc.close } - -nc.publish(subject, msg) -puts "Published [#{subject}] : '#{msg}'" diff --git a/bin/nats-req.cr b/bin/nats-req.cr deleted file mode 100644 index ddea19d..0000000 --- a/bin/nats-req.cr +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright 2019 The NATS Authors -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require "option_parser" -require "../src/nats/connection" - -USAGE = "Usage: nats-req [-s server] " - -def usage - STDERR.puts USAGE; exit(1) -end - -server = NATS::Connection::DEFAULT_URI -args = ARGV.dup - -OptionParser.parse(args) do |parser| - parser.banner = USAGE - parser.on("-s SERVER", "--server=SERVER", "NATS Server to connect") { |url| server = url } - parser.on("-h", "--help", "Show this help") { usage } - parser.invalid_option do |flag| - STDERR.puts "ERROR: #{flag} is not a valid option." - usage - end -end - -usage unless args.size >= 1 -subject, msg = args[0], args[1]? - -nc = NATS::Connection.new(server) -puts nc.request(subject, msg) diff --git a/bin/nats-sub.cr b/bin/nats-sub.cr deleted file mode 100644 index 67102f8..0000000 --- a/bin/nats-sub.cr +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright 2019 The NATS Authors -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require "option_parser" -require "../src/nats/connection" - -USAGE = "Usage: nats-sub [-s server] [-r] [-t] [queue_group]" - -def usage - STDERR.puts USAGE; exit(1) -end - -server = NATS::Connection::DEFAULT_URI -show_time, show_raw = false, false - -args = ARGV.dup -OptionParser.parse(args) do |parser| - parser.banner = USAGE - parser.on("-s SERVER", "--server=SERVER", "NATS Server to connect") { |url| server = url } - parser.on("-t", "--time") { show_time = true } - parser.on("-r", "--raw") { show_raw = true } - parser.on("-h", "--help", "Show this help") { usage } - parser.invalid_option do |flag| - STDERR.puts "ERROR: #{flag} is not a valid option." - usage - end -end - -usage unless args.size >= 1 -subject, queue_group = args[0], args[1]? -index = 0 - -nc = NATS::Connection.new(server) -nc.on_close { STDERR.puts "Connection closed, exiting."; exit(1) } - -nc.subscribe(subject, queue_group) do |msg| - if show_raw - puts msg - else - time_prefix = "[#{Time.now}] " if show_time - puts "#{time_prefix}[\##{index += 1}] Received on [#{msg.subject}] : '#{msg}'" - end -end - -puts "Listening on [#{subject}]" unless show_raw -sleep diff --git a/docs/404.html b/docs/404.html new file mode 100644 index 0000000..ffc6cd6 --- /dev/null +++ b/docs/404.html @@ -0,0 +1,192 @@ + + + + + + + + + + + + + + + + + nats main-dev + + + + + + + + + + +
+

+ 404 Not Found +

+ +

+ This page is unavailable in this version of the API docs. +

+ +

+ You can use the sidebar to search for your page, or try a different + Crystal version. +

+ +
+ + diff --git a/docs/NATS.html b/docs/NATS.html index e595780..da61464 100644 --- a/docs/NATS.html +++ b/docs/NATS.html @@ -1,66 +1,167 @@ - + - + + + + - + - - - NATS - github.com/nats-io/nats.cr + + NATS - nats main-dev + + + +