License: Apache 2.0
The NotificationCenter library provides a unified interface for publish/subscribe (pub/sub) messaging in Go applications. It simplifies asynchronous communication between services in serverless and microservices architectures by abstracting the complexities of various message brokers.
With NotificationCenter, you can seamlessly integrate different pub/sub backends like Kafka, NATS, Redis, PostgreSQL, and more without altering your application logic. This promotes decoupled architectures, enhancing performance, reliability, and scalability.
- Unified Interface: Interact with multiple pub/sub backends using a consistent API.
- Easy Integration: Quickly set up publishers and subscribers with minimal configuration.
- Backend Flexibility: Swap out message brokers without changing your application code.
- Event-Driven Architecture: Facilitate loosely coupled communication between services.
- Scalability: Improve performance and reliability by decoupling application components.
Install the library using go get
:
go get github.com/geniusrabbit/notificationcenter/v2
Below are basic examples demonstrating how to use NotificationCenter in your Go application.
import (
nc "github.com/geniusrabbit/notificationcenter/v2"
)
Create a new publisher using one of the supported backends. For example, using NATS:
import (
"github.com/geniusrabbit/notificationcenter/v2/nats"
"log"
)
// Create a new NATS publisher
eventStream, err := nats.NewPublisher(
nats.WithNatsURL("nats://hostname:4222"),
)
if err != nil {
log.Fatal(err)
}
// Register the publisher with NotificationCenter
err = nc.Register("events", eventStream)
if err != nil {
log.Fatal(err)
}
You can publish messages using global functions or by obtaining a publisher interface.
Using Global Functions:
import (
"context"
)
// Define your message structure
type Message struct {
Title string
}
// Publish a message globally
nc.Publish(context.Background(), "events", Message{Title: "Event 1"})
Using Publisher Interface:
// Get the publisher interface
eventsPublisher := nc.Publisher("events")
// Publish a message
eventsPublisher.Publish(context.Background(), Message{Title: "Event 2"})
Create a subscriber and register it with NotificationCenter.
import (
"context"
"fmt"
"github.com/geniusrabbit/notificationcenter/v2"
"github.com/geniusrabbit/notificationcenter/v2/nats"
"github.com/geniusrabbit/notificationcenter/v2/interval"
"time"
)
func main() {
ctx := context.Background()
// Create a NATS subscriber
eventsSubscriber := nats.MustNewSubscriber(
nats.WithTopics("events"),
nats.WithNatsURL("nats://hostname:4222"),
nats.WithGroupName("group"),
)
nc.Register("events", eventsSubscriber)
// Optional: Create a time interval subscriber (e.g., for periodic tasks)
refreshSubscriber := interval.NewSubscriber(5 * time.Minute)
nc.Register("refresh", refreshSubscriber)
// Subscribe to the "events" stream
nc.Subscribe("events", func(msg nc.Message) error {
// Process the received message
fmt.Printf("Received message: %v\n", msg.Data())
// Acknowledge the message if necessary
return msg.Ack()
})
// Subscribe to the "refresh" stream for periodic tasks
nc.Subscribe("refresh", func(msg nc.Message) error {
// Perform your periodic task here
fmt.Println("Performing periodic refresh")
return msg.Ack()
})
// Start listening for messages
nc.Listen(ctx)
}
- Add support for Amazon SQS
- Add support for Redis queue
- Add support for RabbitMQ
- Add support for MySQL notifications
- Add support for PostgreSQL notifications
-
Remove deprecated metrics from the queue - Add support for NATS & NATS Streaming
- Add support for Kafka queue
- Add support for native Go channels
- Add support for Time Interval Execution
NotificationCenter is licensed under the Apache 2.0 License.
By using NotificationCenter, you can focus on building the core functionality of your application without worrying about the intricacies of different messaging infrastructures. Feel free to contribute to the project or report any issues you encounter.