README
ยถ
Rabbilite
A lightweight and efficient library for working with RabbitMQ in Go applications. Rabbilite provides a simple interface for sending and consuming messages with support for persistence and acknowledgments.
Features
- ๐ Simple and intuitive API
- ๐พ Persistent message support (Persistent)
- โ Automatic message acknowledgment/rejection (Ack/Nack)
- ๐ Message requeue on errors
- ๐งต Thread-safe message processing
- ๐ฆ JSON serialization out of the box
Installation
go get github.com/rabbitmq/amqp091-go
go get github.com/XeshSufferer/Rabbilite
Quick Start
Sending Messages
package main
import (
"log"
rabbitmq "github.com/XeshSufferer/Rabbilite/rabbilite"
)
func main() {
producer, err := rabbitmq.NewProducer("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatal(err)
}
defer producer.Close()
// Send a simple message
err = producer.SendMessage("my_queue", "Hello, World!")
if err != nil {
log.Fatal(err)
}
// Send a struct
type MyMessage struct {
Name string `json:"name"`
Value int `json:"value"`
}
msg := MyMessage{Name: "test", Value: 42}
err = producer.SendMessage("my_queue", msg)
}
Consuming Messages
package main
import (
"log"
rabbitmq "github.com/XeshSufferer/Rabbilite/rabbilite"
)
func main() {
consumer, err := rabbitmq.NewConsumer("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatal(err)
}
defer consumer.Close()
err = consumer.StartConsuming("my_queue", func(message []byte) error {
log.Printf("Received: %s", string(message))
// Your processing logic here
return nil // Return nil acknowledges the message (Ack)
// return errors.New("error") // Return error requeues the message (Nack + requeue)
})
if err != nil {
log.Fatal(err)
}
// Wait indefinitely for messages
select {}
}
API Reference
Producer
NewProducer(url string) (*Producer, error)
Creates a new producer with connection to RabbitMQ.
producer.SendMessage(queueName string, message interface{}) error
Sends a message to the specified queue. Supports any data types (serialized to JSON).
producer.Close()
Closes the connection to RabbitMQ.
Consumer
NewConsumer(url string) (*Consumer, error)
Creates a new consumer with connection to RabbitMQ.
consumer.StartConsuming(queueName string, handler MessageHandler) error
Starts consuming messages from the queue. Handler must return error or nil.
consumer.Close()
Closes the connection to RabbitMQ.
MessageHandler Type
type MessageHandler func(message []byte) error
Usage Example
// producer.go
producer.SendMessage("tasks", map[string]interface{}{
"task_id": 123,
"action": "process",
"data": "...",
})
// consumer.go
consumer.StartConsuming("tasks", func(message []byte) error {
var task map[string]interface{}
if err := json.Unmarshal(message, &task); err != nil {
return err // Message will be requeued
}
// Process the task
fmt.Printf("Processing task: %v\n", task)
return nil // Message acknowledged
})
RabbitMQ Configuration
The library works with standard RabbitMQ settings. For production use ensure:
- Virtual host and user exist
- Appropriate permissions are granted
- Queue persistence is configured (already enabled in the library)
Development
Testing
# Run RabbitMQ in Docker
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
# Run tests
go test ./...
Contributing
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
License
MIT License. See LICENSE file for details.
Support
If you have questions or suggestions, create an issue in the repository or contact on Telegram: @byxesh
RabbiLite - Making RabbitMQ in Go simple and efficient! ๐โจ