nats
Version, currently 0.1.25 versions
- 0.1.5latestFeb 4, 2021
- 0.1.4not indexedJan 20, 2025
- 0.1.3not indexedJan 20, 2025
- 0.1.2not indexedJan 20, 2025
- 0.1.1not indexedJan 20, 2025
github.com/SlayerShadow/crystal-nats
Crystal Language client library for NATS. https://nats.io
Nothing has been indexed for 0.1.2 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.
Installation
# Add this to your shard.yml
dependencies:
nats:
github: SlayerShadow/crystal-nats
version: ~> 0.1.2Then run:
shards installshard.yml
No shard.yml has been indexed for 0.1.2. You can read it on the repository.
Dependencies
Unknown: the shard.yml for this version has not been read yet.
README
This README is the one indexed from the repository at its latest ref, not from the tag for this version.
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
- Because of the module doesn't know which data will be received (text? binary?), the incoming "message" type is always set to Bytes (see Crystal Thread for more details).
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:
- If NATS launched with
-tlsverify- It should be also launched with at least
-tlscert,-tlskeyand-tlscacert. - App should be started with at least
:cert_key_fileand:ca_cert_file.
- It should be also launched with at least
- If NATS launched without
tlsverify- It can be also launched without
-tlscacert. - App can be started even without
:tlskey (protocol will be switched to TLS transparently).
- It can be also launched without
- If app launches with
:verify_peer => true- NATS can be launched without
-tlscacertbut the-tlscertand-tlskeyshould be passed. - App should be started with
:ca_cert_fileto verify NATS. - App can be started without
:cert_fileand:cert_key_file(only CA is mandatory).
- NATS can be launched without
- If NATS launched with
-tlsverifyand app launches with:verify_peer => true- NATS and app should be fully configured for TLS connection.
Development
Features
- [x] Exchange server messages
- [ ] Tests
- [x] Clusterization
- [ ] Tests
- [x] Publish/Subscribe
- [ ] on_error callback
- [ ] on_disconnect callback
- [ ] on_reconnect callback
- [ ] on_close callback
- [ ] Tests
- [x] Unsubscribe
- [ ] Unsubscribe after receiving amount of messages
- [ ] Tests
- [x] Reconnect after critical errors
- [ ] Tests
- [x] User authentication
- [ ] Tests
- [x] OpenSSL
- [x] Connect with using certificates
- [x] Manage server information to determine when to connect through SSL
- [ ] Tests
- [ ] Subscriptions timeouts
- [ ] Tests
Improvements
- [ ] Make the "options" flexible
- [x] Hash
- [ ] JSON::Any
- [ ] YAML::Any
- [ ] Require yaml in macros and only when it needed (lazy)
- [ ] Add configuration for channel buffer (see Crystal thread)
Performance
- [ ] Remove regex from parser
- [ ] Manage
Bytesonly, even when receive protocol commands
Contributing
- Fork it ( https://github.com/SlayerShadow/crystal-nats/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- SlayerShadow Dmitry Lykov - creator, maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.1.2- Tagged
- Jan 20, 2025
- Commit
6faaf36f92ac- Indexed
- not yet
Dependents
No indexed shard depends on this one yet.
Repository
github.com/SlayerShadow/crystal-nats
Metadata
- Created
- Aug 12, 2026
- Updated
- Aug 13, 2026
- Synced
- Aug 13, 2026
- Versions
- 5