{"id":37175372,"url":"https://github.com/syntaxerrorlinenull/nats-pubsub-go","last_synced_at":"2026-01-14T20:28:41.895Z","repository":{"id":255247891,"uuid":"848995301","full_name":"SyntaxErrorLineNULL/nats-pubsub-go","owner":"SyntaxErrorLineNULL","description":"nats-pubsub-go is a Go library that provides a simple and efficient interface for interacting with NATS, a lightweight, high-performance messaging system. This library simplifies the process of publishing and subscribing to messages on NATS subjects, making it easy to build scalable and distributed applications.","archived":false,"fork":false,"pushed_at":"2025-06-24T11:52:01.000Z","size":121,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-24T12:49:47.960Z","etag":null,"topics":["go-nats","golang","nats","nats-pubsub","publish-subscribe","pubsub"],"latest_commit_sha":null,"homepage":"","language":"Go","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/SyntaxErrorLineNULL.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,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-08-28T19:31:38.000Z","updated_at":"2025-06-24T11:52:05.000Z","dependencies_parsed_at":"2024-08-28T21:16:39.449Z","dependency_job_id":"d05f8db7-44d3-4cc4-b3ea-e275e69e1f1f","html_url":"https://github.com/SyntaxErrorLineNULL/nats-pubsub-go","commit_stats":null,"previous_names":["syntaxerrorlinenull/nats-pubsub-go"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/SyntaxErrorLineNULL/nats-pubsub-go","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SyntaxErrorLineNULL%2Fnats-pubsub-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SyntaxErrorLineNULL%2Fnats-pubsub-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SyntaxErrorLineNULL%2Fnats-pubsub-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SyntaxErrorLineNULL%2Fnats-pubsub-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SyntaxErrorLineNULL","download_url":"https://codeload.github.com/SyntaxErrorLineNULL/nats-pubsub-go/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SyntaxErrorLineNULL%2Fnats-pubsub-go/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28434459,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T18:57:19.464Z","status":"ssl_error","status_checked_at":"2026-01-14T18:52:48.501Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["go-nats","golang","nats","nats-pubsub","publish-subscribe","pubsub"],"created_at":"2026-01-14T20:28:41.052Z","updated_at":"2026-01-14T20:28:41.882Z","avatar_url":"https://github.com/SyntaxErrorLineNULL.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nats-pubsub-go\nnats-pubsub-go is a Go library that provides a simple and efficient interface for interacting with NATS, a lightweight, high-performance messaging system. This library simplifies the process of publishing and subscribing to messages on NATS subjects, making it easy to build scalable and distributed applications.\n\n### Installation\n```bash\ngo get -u github.com/SyntaxErrorLineNULL/nats-pubsub-go\n```\n\n# Publisher Package\n\nThis package provides a Publisher struct for sending messages and requests to a NATS server. It ensures safe and efficient communication, handling connection state and message validation.\n\n### Features\n\n* **Reliable Publishing**: Publishes messages to a NATS server with error handling and flushing for immediate delivery.\n* **Request-Response**: Sends message requests with a timeout and retrieves responses.\n* **Connection Management**: Tracks the connection state to prevent operations on closed connections.\n* **Message Validation**: Ensures messages are valid and not nil before publishing.\n* **Error Handling**: Returns specific errors for different failure scenarios, aiding in debugging.\n\n## Usage\n\n### Create a Publisher:\n```go\n// Establish a connection to the NATS server (replace with your connection details)\nconn, err := nats.Connect(...)\nif err != nil {\n    // Handle connection error\n}\n\n// Create a Publisher instance\npub := publisher.NewPublisher(conn)\n```\n\n### Publish Messages:\n```go\n// Message to be published\nmsg := \u0026nats.Msg{Subject: \"my_subject\", Data: []byte(\"Hello, world!\")}\n\n// Publish a single message\nerr = pub.Publish(msg)\nif err != nil {\n    // Handle publishing error (e.g., connection closed, invalid message)\n}\n\n// Publish multiple messages in a batch\nmessages := []*nats.Msg{msg, anotherMessage}\nerr = pub.Publish(messages...)\nif err != nil {\n    // Handle publishing error\n}\n```\n\n### Send Request and Receive Response:\n```go\n// Define the subject for the message to be published.\n// The subject acts as a channel or topic to which the message will be sent.\n// In this test, the subject is set to \"test.subject\".\nsubject := \"test.subject\"\n\n// Create a buffered channel with a capacity of 1, which will be used to signal when the response is received.\n// The channel `resCh` is of type `chan struct{}`, which is commonly used for signaling without carrying any data.\nresCh := make(chan struct{}, 1)\n\n// Ensure that the channel `resCh` is closed when the test function exits.\n// `defer close(resCh)` schedules the closing of the channel to happen after the function completes,\n// which helps in cleaning up resources and avoiding potential memory leaks.\ndefer close(resCh)\n\n// Define the expected request message to be sent to the PubSub system.\n// This byte slice represents the data that should be sent with the request message.\nexpectedRequestMessage := []byte(\"test_request\")\n\n// Define the expected publish message that should be received in response to the request.\n// This byte slice represents the data that the publisher should send as a reply.\nexpectedPublishMessage := []byte(\"test_publish\")\n\n// Subscribe to the subject using the NATS connection.\n// This simulates a service that listens for the request and sends back a response.\n_, _ = natsConnection.Subscribe(subject, func(msg *nats.Msg) {\n    // Publish the expected response message to the reply subject specified in the incoming message.\n    // The `msg.Reply` field contains the subject where the response should be sent.\n    // `expectedPublishMessage` is the data payload that will be sent as the response.\n    err = natsConnection.Publish(msg.Reply, expectedPublishMessage)\n    if err != nil { \n        log.Println(\"failed publish\")\n        return\n    }\n\n    // Signal that the response has been published by sending a value to the result channel (resCh).\n    resCh \u003c- struct{}{}\n})\n\n// Send a request message to the NATS server using the `publisher.Request` method.\n// The request message is constructed with the specified `subject` and `expectedRequestMessage` data.\n// A timeout of 1 second is provided for the request, meaning the request will wait up to 1 second for a response.\nresponse, err := publisher.Request(\u0026nats.Msg{Subject: subject, Data: expectedRequestMessage}, 1*time.Second)\nif err != nil { log.Println(\"failed request\") }\n\n// Use a select statement to handle multiple cases: receiving a response or timing out.\nselect {\n// Case when a response message is received on the `resCh` channel.\n// The response channel `resCh` is signaled when the expected publish message is received.\ncase \u003c-resCh:\n    ...\n    // Exit the loop and the test since the expected response has been successfully received and validated.\n    return\n\n// Case when the timeout duration (5 seconds) is reached without receiving a response.\n// The `\u003c-time.After(5 * time.Second)` statement waits for 5 seconds and then proceeds.\ncase \u003c-time.After(5 * time.Second):\n    // Fail the test with a timeout error if no response was received within the 5-second window.\n    // The `t.Fatal` function logs the provided message and stops the test execution.\n    log.Println(\"Timed out waiting for message\")\n}\n```\n\n### Close the Publisher:\n```go\n// Close the connection and mark the Publisher as closed\npub.Close()\n\n// Do not attempt to publish after closing the Publisher\n```\n\n## Error Handling\n\nThe package returns specific errors for different failure scenarios:\n\n```go \nnats_pubsub_go.ErrCloseConnection // The Publisher is closed and cannot be used for further operations.\nnats_pubsub_go.ErrInvalidArgument // An invalid argument was provided, such as empty messages or a nil request message.\n```\n\n# Subscribe Package\n\nThis package provides robust tools for subscribing to messages on a NATS server. It offers functions for both asynchronous and synchronous subscriptions, ensuring flexibility and control over message processing.\n\n### Features\n\n* **Multiple Subscription Modes**: Supports asynchronous and synchronous subscriptions depending on your needs.\n* **Error Handling**: Handles errors during subscription creation and message retrieval, providing informative error messages.\n* **Safe Unsubscription**: Guarantees that the subscription is unsubscribed from and the message channel is closed only once.\n* **Clean Channel Access**: Provides methods to retrieve the message channel for asynchronous subscriptions and receive messages for synchronous subscriptions.\n\n### Create a Subscriber:\n\n```go\n// Establish a connection to the NATS server (replace with your connection details)\nconn, err := nats.Connect(...)\nif err != nil {\n    // Handle connection error\n}\n\n// Create a new Subscriber instance with the NATS connection\nsubscriber := pkg.NewSubscriber(conn)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntaxerrorlinenull%2Fnats-pubsub-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntaxerrorlinenull%2Fnats-pubsub-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntaxerrorlinenull%2Fnats-pubsub-go/lists"}