{"id":21476053,"url":"https://github.com/n25a/gorabbit","last_synced_at":"2025-07-15T10:31:52.405Z","repository":{"id":211408287,"uuid":"729050512","full_name":"n25a/gorabbit","owner":"n25a","description":"RabbitMQ client for Golang","archived":false,"fork":false,"pushed_at":"2024-06-12T19:31:35.000Z","size":2335,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-14T01:48:11.434Z","etag":null,"topics":["client","rabbit-mq","rabbitmq"],"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/n25a.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","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}},"created_at":"2023-12-08T09:45:06.000Z","updated_at":"2024-06-12T19:31:38.000Z","dependencies_parsed_at":"2024-06-13T01:34:26.023Z","dependency_job_id":"f7b3f872-531e-4bfd-92b0-64676bfc8a17","html_url":"https://github.com/n25a/gorabbit","commit_stats":null,"previous_names":["n25a/gorabbit"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n25a%2Fgorabbit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n25a%2Fgorabbit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n25a%2Fgorabbit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n25a%2Fgorabbit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/n25a","download_url":"https://codeload.github.com/n25a/gorabbit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226033272,"owners_count":17563125,"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":["client","rabbit-mq","rabbitmq"],"created_at":"2024-11-23T10:46:51.760Z","updated_at":"2024-11-23T10:46:52.202Z","avatar_url":"https://github.com/n25a.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"![logo](https://raw.githubusercontent.com/n25a/gorabbit/master/docs/logo.jpg)\n\n\n# 🐇 gorabbit - RabbitMQ client for Golang\n\n[![GoDoc](https://godoc.org/github.com/n25a/gorabbit?status.svg)](https://godoc.org/github.com/n25a/gorabbit)\n![License](https://img.shields.io/badge/license-MIT-blue.svg)\n\n\n## 📖 Introduction\n\ngorabbit is a RabbitMQ client for Golang. It is a wrapper around [rabbitmq/amqp091-go](github.com/rabbitmq/amqp091-go) library. \nIt provides a simple interface to interact with RabbitMQ. Also, it provides a simple way to create a consumer and a publisher that we call `jobs`.\nIt's good to mention that gorabbit handles the reconnection and reconsuming the jobs automatically.\n\n\n## 📦 Installation\n\n```bash\ngo get github.com/n25a/gorabbit\n```\n\n\n## ⚙️ How to use\n\nIn the following, we will show you how to use gorabbit in your project.\n\n### 🐇 Create a connection\n\nIn the first step, you should create a rabbitmq instance. Then, you can make a connection to the rabbitmq server.\n\n```go\nimport (\n    \"github.com/n25a/gorabbit\"\n)\n\nfunc main() {\n    // Create a rabbitmq instance \n    rabbit := gorabbit.NewRabbitMQ(\n        dsn,\n        dialTimeout,\n        dialRetry,\n        ctxTimeout,\n        logLevel,\n    ) \n    \n    // Create a connection\n    err := rabbit.Connect()\n    if err != nil {\n        panic(err)\n    }\n    \n    // Close the connection\n    defer rabbit.Close()\n}\n```\n\nEach parameter of the `NewRabbitMQ` function is described below:  \n* dsn: RabbitMQ connection string. It should be in the following format: `amqp://user:password@host:port/vhost`\n* dialTimeout: The timeout for dialing to the RabbitMQ server. The default value is 5 seconds.\n* dialRetry: The number of retries for dialing the RabbitMQ server. The default value is 0.\n* ctxTimeout: The timeout for the context of the consumer handler. The default value is 1 second.\n* logLevel: The log level for the gorabbit. The default value is `info`. It can be `debug`, `info`, `warn`, `error`, `fatal`, `panic`, `dpanic`.\n\nThis function creates an instance of the `RabbitMQ` struct. This struct has the following methods:\n* Connect: Create a connection to the RabbitMQ server.\n* Close: Close the connection to the RabbitMQ server.\n* Declare: Declare a queue and an exchange.\n* StartConsumingJobs: Start the consumers.\n* NewJob: Create a new job instance.\n* ShutdownJobs: Shutdown the consumers.\n* GetConnection: Get the connection to the RabbitMQ server.\n* GetChannel: Get the channel to the RabbitMQ server.\n\nFor connecting to the RabbitMQ server, you should call the `Connect` method. Also, you should call the `Close` method for closing the connection.\n\n\n### ✍️ Declare a queue and an exchange\n\nYou should call the `Declare` method for declaring a queue and an exchange. This method has the following parameters:\n* exchangeOption: The exchange options. The `ExchangeDeclareOption` function can create it.\n* queueOption: The queue options. The `QueueDeclareOption` function can create it. It's good to mention that you can pass multiple queue options to this function for the exact exchange.\n\n```go\nimport (\n    \"github.com/n25a/gorabbit\"\n)\n\nfunc main() {\n    //...\n    \n    // Close the connection\n    defer rabbit.Close()\n    \n    \n    // Declare an exchange \n    exchangeOption := gorabbit.ExchangeDeclareOption(\n        name,\n        kind,\n        durable,\n        autoDelete,\n        internal,\n        noWait,\n        args,\n    )\n\t\n    // Declare a queue\n    queueOption := gorabbit.QueueDeclareOption(\n        name,\n        durable,\n        autoDelete,\n        exclusive,\n        noWait,\n        args,\n    )\n    \n    // Declare a queue and an exchange for rabbit instance\n    err = rabbit.Declare(exchangeOption, queueOption)\n    if err != nil {\n        panic(err)\n    }\n}\n```\n\n### 📩 Consume a job\n\nTo create a job instance, you should create a job instance. Then, you should call the `StartConsumingJobs` method for starting the consumers.\nThis method consumes all jobs created by `gorabbit.RabbitMQ` instance.\n\nCreating a new `job` instance has the following parameters:\n* handler: The handler function for consuming the job. It should be in the format: `func(ctx context.Context, message []byte) error`.\n* jobExchange: The exchange name for consuming the job.\n* jobQueue: The queue name for consuming the job.\n* autoAck: The auto ack for consuming the job.\n* justPublish: It's a flag for just publishing the job. It helps start all consumers and pass publishers' jobs.\n\n```go\nimport (\n    \"github.com/n25a/gorabbit\"\n)\n\nfunc main() {\n    //...\n    \n    // Declare a queue and an exchange for rabbit instance\n    err = rabbit.Declare(exchangeOption, queueOption)\n    if err != nil {\n        panic(err)\n    }\n\t\n    // create a job instance\n\tjob := rabbit.NewJob(\n        handler,\n        jobExchange,\n        jobQueue,\n        autoAck,\n        justPublish,\n    )\n\t\n    // Create a consumers\n    err = rabbit.StartConsumingJobs()\n    if err != nil {\n        panic(err)\n    }\n}\n```\n\n### 📨 Publish a job\n\nTo publish a job, you should create a job instance. Then, you should call the `Publish` method for publishing the job.\nThis method, publishes the message on the declared job to the RabbitMQ server.\nThe `Publish` method has the following parameters:\n* ctx: The context for publishing the job.\n* message: The message for publishing the job. It should be in the `[]byte` format.\n* options: The options for publishing the job. The `PublishOption` function can create it. These optional options are described below:\n    * WithContentType: The content type for publishing the job. The default value is `text/json`.\n    * WithDelay: The delay in publishing the job. The default value is 0.\n    * WithPriority: The priority for publishing the job. The default value is 0.\n\n```go\nimport (\n    \"github.com/n25a/gorabbit\"\n)\n\nfunc main() {\n    //...\n\n    // Create a consumer\n    err = rabbit.StartConsumingJobs()\n    if err != nil {\n        panic(err)\n    }\n\t\n    // Create a publisher\n    msg, _ := json.Marshal(struct{\n        Body string `json:\"body\"`\n    }{\n        Body: \"test\",\n    })\n\t\n    err = job.Publish(\n        context.Background(),\n        msg,\n        gorabbit.WithContentType(\"text/json\"),\n        gorabbit.WithDelay(10),\n        gorabbit.WithPriority(0),\n    )\n    if err != nil {\n        panic(err)\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn25a%2Fgorabbit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fn25a%2Fgorabbit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn25a%2Fgorabbit/lists"}