{"id":15370382,"url":"https://github.com/jedisct1/rust-nats","last_synced_at":"2025-04-14T21:53:21.304Z","repository":{"id":52415319,"uuid":"42080677","full_name":"jedisct1/rust-nats","owner":"jedisct1","description":"A simple NATS client library for Rust","archived":false,"fork":false,"pushed_at":"2021-04-30T14:04:39.000Z","size":77,"stargazers_count":103,"open_issues_count":8,"forks_count":18,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-14T21:53:14.330Z","etag":null,"topics":["nats","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jedisct1.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}},"created_at":"2015-09-08T00:24:46.000Z","updated_at":"2024-11-24T06:10:08.000Z","dependencies_parsed_at":"2022-09-06T06:00:47.652Z","dependency_job_id":null,"html_url":"https://github.com/jedisct1/rust-nats","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedisct1%2Frust-nats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedisct1%2Frust-nats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedisct1%2Frust-nats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jedisct1%2Frust-nats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jedisct1","download_url":"https://codeload.github.com/jedisct1/rust-nats/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248968754,"owners_count":21191158,"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":["nats","rust"],"created_at":"2024-10-01T13:41:28.589Z","updated_at":"2025-04-14T21:53:21.286Z","avatar_url":"https://github.com/jedisct1.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rust-NATS\n\nRust-NATS is a Rust client library for the [NATS](https://nats.io) message\nqueue.\n\nThe crate is called `nats` and can be added as a dependency using Cargo:\n\n```rust\n[dependencies]\nnats = \"*\"\n```\n\nRust -stable, -beta and -nightly are supported.\n\nThe library was designed to be robust. It doesn't use any usafe code, it never\ncalls `panic!()` and failed commands are automatically retried on different\ncluster nodes.\n\nIt provides a simple low-level interface that makes it easy to send/receive\nmessages over Rust channels if needed.\n\n# Connecting\n\nSingle-node client connection:\n\n```rust\nextern crate nats;\nuse nats::*;\n\nlet client = Client::new(\"nats://user:password@127.0.0.1\").unwrap();\n```\n\nThe username and password are optional.\n\nConnecting to a cluster:\n\n```rust\nlet cluster = vec!(\"nats://user:password@127.0.0.1\", \"nats://127.0.0.2\");\nlet mut client = nats::Client::new(cluster).unwrap();\n```\n\nBy default, commands are sent in fire-and-forget mode. In order to wait for an\nacknowledgment after each command, the synchronous (\"verbose\") mode can be\nturned on:\n\n```rust\nclient.set_synchronous(true);\n```\n\nThe client name can also be customized:\n\n```rust\nclient.set_name(\"app\");\n```\n\n# Publishing messages\n\n```rust\nclient.publish(\"subject.test\", \"test\".as_bytes()).unwrap();\n```\n\nIn order to use NATS for RPC, the `Client.make_request()` function creates an\nephemeral subject (\"inbox\"), subscribes to it, schedules the removal of the\nsubscription after the first received message, publishes the initial request,\nand returns the inbox subject name:\n\n```rust\nlet inbox = client.make_request(\"subject.rpc\", \"test\".as_bytes()).unwrap();\n```\n\n# Subscribing to subjects\n\n`Client.subscribe()` adds a subscription to a subject, with an optional group:\n\n```rust\nlet s1 = client.subscribe(\"subject\", None).unwrap();\nlet s2 = client.subscribe(\"subject.*\", Some(\"app\")).unwrap();\n```\n\nWith group membership, a given message will be only delivered to one client in\nthe group.\n\n`Client.unsubscribe()` removes a subscription:\n\n```rust\nclient.unsubscribe(s1).unwrap();\n```\n\nOr to remove it after `n` messages have been received:\n\n```rust\nclient.unsubscribe_after(s1, n).unwrap();\n```\n\n# Receiving events\n\n`Client.wait()` waits for a new event, and transparently responds to server\n`PING` requests.\n\n```rust\nlet event = client.wait();\n```\n\nThis returns an `Event` structure:\n\n```rust\npub struct Event {\n    pub subject: String,\n    pub channel: Channel,\n    pub msg: Vec\u003cu8\u003e,\n    pub inbox: Option\u003cString\u003e\n}\n```\n\nAlternatively, events can be received using an iterator:\n\n```rust\nfor event in client.events() {\n    ...\n}\n```\n\n# TLS\n\nBuild and set `TLSConfig` before connect:\n\n```rust\nuse std::fs::File;\nuse std::io::Read;\nuse self::nats::openssl;  // use re-exported openssl crate\n\n// Load root certificate\nlet mut file = File::open(\"./configs/certs/ca.pem\").unwrap();\nlet mut contents = vec![];\nfile.read_to_end(\u0026mut contents).unwrap();\nlet cert = openssl::x509::X509::from_pem(\u0026contents).unwrap();\n\n// Load client certificate\nlet mut file = File::open(\"./configs/certs/client.crt\").unwrap();\nlet mut contents = vec![];\nfile.read_to_end(\u0026mut contents).unwrap();\nlet client_cert = openssl::x509::X509::from_pem(\u0026contents).unwrap();\n\n// Load client key\nlet mut file = File::open(\"./configs/certs/client.key\").unwrap();\nlet mut contents = vec![];\nfile.read_to_end(\u0026mut contents).unwrap();\nlet client_key = openssl::pkey::PKey::private_key_from_pem(\u0026contents).unwrap();\n\nlet mut builder = nats::TlsConfigBuilder::new().unwrap();\n// Set root certificate\nbuilder.add_root_certificate(cert).unwrap();\n// Set client certificate\nbuilder.add_client_certificate(\u0026client_cert, \u0026client_key).unwrap();\nlet tls_config = builder.build();\n\nlet mut client = nats::Client::new(\"nats://localhost:4222\").unwrap();\nclient.set_tls_config(tls_config);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjedisct1%2Frust-nats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjedisct1%2Frust-nats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjedisct1%2Frust-nats/lists"}