{"id":17681758,"url":"https://github.com/jgaskins/nats","last_synced_at":"2025-05-12T14:11:55.231Z","repository":{"id":43046029,"uuid":"310791163","full_name":"jgaskins/nats","owner":"jgaskins","description":"NATS client in pure Crystal with JetStream support","archived":false,"fork":false,"pushed_at":"2025-03-25T14:44:38.000Z","size":933,"stargazers_count":22,"open_issues_count":4,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-31T22:51:06.828Z","etag":null,"topics":["cloud-native","crystal","jetstream","message-broker","message-bus","message-queue","messaging","microservices","microservices-architecture","nats"],"latest_commit_sha":null,"homepage":"https://jgaskins.dev/nats","language":"Crystal","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jgaskins.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-11-07T07:27:45.000Z","updated_at":"2025-03-25T14:44:42.000Z","dependencies_parsed_at":"2023-12-06T05:42:57.160Z","dependency_job_id":null,"html_url":"https://github.com/jgaskins/nats","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgaskins%2Fnats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgaskins%2Fnats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgaskins%2Fnats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgaskins%2Fnats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jgaskins","download_url":"https://codeload.github.com/jgaskins/nats/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246615772,"owners_count":20806005,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloud-native","crystal","jetstream","message-broker","message-bus","message-queue","messaging","microservices","microservices-architecture","nats"],"created_at":"2024-10-24T09:12:07.448Z","updated_at":"2025-04-01T09:30:29.987Z","avatar_url":"https://github.com/jgaskins.png","language":"Crystal","funding_links":[],"categories":["Clients"],"sub_categories":["Community Clients"],"readme":"# NATS\n\nNATS is a message broker for distributed systems.\n\n## Installation\n\n1. Add the dependency to your `shard.yml`:\n\n   ```yaml\n   dependencies:\n     nats:\n       github: jgaskins/nats\n   ```\n\n2. Run `shards install`\n\n## Usage\n\nYou can use NATS in a publish/subscribe or request/reply paradigm.\n\n### Publish/Subscribe\n\nFor publish/subscribe, let's consider the following class to be shared, representing an event that will be published by one service and picked up by another:\n\n```crystal\nrequire \"uuid\"\nrequire \"json\"\nrequire \"uuid/json\"\n\nstruct UserRegisteredEvent\n  include JSON::Serializable\n\n  getter id : UUID\n  getter email : String\n  getter name : String\n\n  def initialize(@id, @email, @name)\n  end\nend\n```\n\nIn one service, we can subscribe to a subject that will be sent all of the events pertaining to a user registering:\n\n```crystal\nrequire \"nats\"\n\nnats = NATS::Client.new(URI.parse(ENV[\"NATS_URL\"]))\n\n# Subscribe to all messages on \"customers.registration\" with an optional queue\n# group. A message will only be delivered to a single client in a given queue\n# group.\nnats.subscribe \"customers.registration\", queue_group: \"cart-service\" do |msg|\n  new_user = UserRegisteredEvent.from_json(msg.body_io)\n\n  # This message represents that a new customer has registered, presumably sent\n  # by our identity/authentication/user service. We create a record for this\n  # customer in our own database so we don't always need to request the info\n  # from that service.\n  UserQuery.new.create_from_message(new_user)\nend\n\n# Accept wildcard messages. This would match:\n# - orders.commercial.fulfilled\n# - orders.individual.fulfilled\nnats.subscribe \"orders.*.shipped\", queue_group: \"cart-service\" do |msg|\n  # ...\nend\n\n# Since the subscribe blocks above do not block execution, we need to keep the\n# main fiber from exiting. In a real-world app, you might trap a TERM/INT signal\n# to allow the app to close the connection gracefully.\nsleep\n```\n\nAnd then to publish on those topics:\n\n```crystal\nrequire \"nats\"\n\nnats = NATS::Client.new(URI.parse(ENV[\"NATS_URL\"]))\n\n# We can publish a message with a given subject. In this example, we'll\n# publish a message saying Jolene has registered.\nnats.publish \"customers.registration\", UserRegisteredEvent.new(\n  id: UUID.random,\n  name: \"Jolene\",\n  email: \"jolene@gonnatakeyourman.com\",\n)\n\nnats.close\n```\n\n### Request/Reply\n\nLet's consider an orders service that we may want to send requests to.\n\n```crystal\nrequire \"uuid\"\nrequire \"json\"\nrequire \"uuid/json\"\nrequire \"db\"\n\nmodule Orders\n  struct Get\n    include JSON::Serializable\n\n    getter id : UUID\n\n    def initialize(@id)\n    end\n  end\nend\n\nstruct Order\n  include DB::Serializable\n  include JSON::Serializable\n\n  getter id : UUID\n  getter address : String\n  getter city : String\n  getter state : String\n  getter postal_code : String\nend\n```\n\n#### Define the request handler\n\n```crystal\nrequire \"nats\"\n\nnats = NATS::Client.new(URI.parse(ENV[\"NATS_URL\"]))\n\n# Subscribe to the subject that the request will be sent to\nnats.subscribe \"orders.get\", do |msg|\n  request = Orders::Get.from_json(msg.body_io)\n\n  order = OrderQuery.new.with_id(request.id)\n\n  nats.reply msg, order.to_json\nend\n```\n\n#### Sending the request\n\n```crystal\nrequire \"nats\"\n\nnats = NATS::Client.new(URI.parse(ENV[\"NATS_URL\"]))\n\n# Send the request to the subject it is expected to be received on:\norder = nats.request \"orders.get\",\n  message: Orders::Get.new(order_id).to_json,\n  timeout: 5.seconds # A timeout must be specified\n\npp order\n```\n\n## Development\n\nTODO: Write development instructions here\n\n## Contributing\n\n1. Fork it (\u003chttps://github.com/jgaskins/nats/fork\u003e)\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\n## Contributors\n\n- [Jamie Gaskins](https://github.com/jgaskins) - creator and maintainer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgaskins%2Fnats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjgaskins%2Fnats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgaskins%2Fnats/lists"}