Crystal NATS

The Crystal client for the NATS Messaging System.

Installation

Add this to your application's shard.yml:

dependencies:
  nats:
    github: SlayerShadow/crystal-nats

Basic usage

require "nats"

client = NATS::Client.new
# There can be passed options (see below)
client.connect

# Simple subscription (message: Bytes)
client.subscribe("foo"){ |message| puts "Message: #{ String.new message }" }

# Unsubscribe
sid = client.subscribe("foo"){ |message| puts "Message: #{ String.new message }" }
cluent.unsubscribe sid

# Reply (message: Bytes, reply: String)
client.subscribe("foo"){ |message, reply| puts "Replying to: #{ reply }" }

# Matches subject (message: Bytes, reply: String, subject: String)
client.subscribe("foo.*"){|message, reply, subject|
  puts "Receiving from: #{ subject }"
}

# Publish into the channel (channel: String, message: String)
client.publish("foo", "bar")

# Publish with reply (channel: String, message: String, reply: String)
client.publish("foo", "bar", "foo.repl")

Clustered usage (not fully implemented)

require "nats"

options = NATS::Client::OptionsHash{
  :servers => [ "nats://localhost:4222", "nats://localhost:4223" ],
  :randomize_servers => true, # Should servers be chosen randomly
  :reconnect => true, # Tries to reconnect to server after failed attempt
  :max_reconnect_attempts => 10u8, # How many times try to reconnect before exception
  :reconnect_time_wait => 2u8, # Delay between reconnection tries
  :verbose => true # if need to confirm that sent is succeeded
}

client = NATS::Client.new
client.connect options

client.subscribe("foo"){|message, reply, subject|
  # Do something
}

Authentication

require "nats"

options = NATS::Client::OptionsHash{
  :servers => [ "nats://localhost:4222" ],
  :user => "name",
  :pass => "helloworld"
}

client = NATS::Client.new
client.connect options

TLS

require "nats"

options = NATS::Client::OptionsHash{
  :servers => [ "tls://localhost:4444" ],
  :tls => {
    :cert_file => "config/cert.pem",
    :cert_key_file => "config/cert/key.pem",
    :ca_cert_file => "config/ca.pem", # When need to verify peer
    :verify_peer => false             # Along with ca_cert_file - when need to verify peer (default - false)
  }
}

client = NATS::Client.new
client.connect options

Note that:

Development

Features

Improvements

Performance

Contributing

  1. Fork it ( https://github.com/SlayerShadow/crystal-nats/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors