https://github.com/tinh-tinh/pubsub
🔔 Pubsub for Tinh Tinh framework
https://github.com/tinh-tinh/pubsub
framework golang pubsub
Last synced: 5 months ago
JSON representation
🔔 Pubsub for Tinh Tinh framework
- Host: GitHub
- URL: https://github.com/tinh-tinh/pubsub
- Owner: tinh-tinh
- License: mit
- Created: 2024-10-15T13:58:29.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-09-01T12:45:22.000Z (10 months ago)
- Last Synced: 2025-09-01T14:38:24.583Z (10 months ago)
- Topics: framework, golang, pubsub
- Language: Go
- Homepage: https://tinh-tinh.github.io/docs/docs/intergrations/pubsub
- Size: 45.9 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PubSub for Tinh Tinh
## Overview
PubSub is a modular, in-memory publish/subscribe (pubsub) system for the Tinh Tinh framework. It enables decoupled communication between different parts of your application using topics and message subscribers.
## Install
```bash
go get -u github.com/tinh-tinh/pubsub/v2
```
## Features
- **Central Broker:** Manages topics and subscribers.
- **Dynamic Subscribers:** Subscribe to one or many topics dynamically.
- **Asynchronous Messaging:** Message delivery is non-blocking.
- **Topic Patterns:** Supports wildcards and topic delimiters for pattern-based subscriptions.
- **Broadcast Support:** Broadcast a message to all subscribers.
- **Integration with Tinh Tinh Modules:** Use dependency injection for broker and subscribers.
- **Subscriber Limits:** Optionally limit the max number of subscribers.
- **Handler Utility:** Simplifies listening to topics with concise functions and is the recommended way to consume messages.
## Basic Usage
### 1. Register the Broker
```go
import "github.com/tinh-tinh/pubsub/v2"
pubsubModule := pubsub.ForRoot(pubsub.BrokerOptions{
// Optional: MaxSubscribers, Wildcard, Delimiter, etc.
})
```
### 2. Register Subscribers (Feature Modules)
Subscribe to specific topics:
```go
priceSubModule := pubsub.ForFeature("BTC", "ETH") // subscribe to multiple topics
```
### 3. Use Handler for Consumption (Recommended)
**Instead of using `subscriber.GetMessages()` or `Listener`, use the Handler utility for subscribing to topics and consuming messages.**
```go
import (
"github.com/tinh-tinh/pubsub/v2"
"github.com/tinh-tinh/tinhtinh/v2/core"
)
type PriceService struct {
Message interface{}
}
priceService := func(module core.Module) core.Provider {
return module.NewProvider(core.ProviderOptions{
Name: "PriceService",
Value: &PriceService{},
})
}
priceHandler := func(module core.Module) core.Provider {
handler := pubsub.NewHandler(module)
priceService := module.Ref("PriceService").(*PriceService)
handler.Listen(func(msg *pubsub.Message) {
priceService.Message = msg.GetContent()
}, "BTC", "ETH", "SOL")
return handler
}
```
### 4. Use in Controllers
```go
controller := func(module core.Module) core.Controller {
ctrl := module.NewController("prices")
ctrl.Post("", func(ctx core.Ctx) error {
broker := pubsub.InjectBroker(module)
go broker.Publish("BTC", "hihi")
return ctx.JSON(core.Map{"data": "ok"})
})
ctrl.Get("", func(ctx core.Ctx) error {
service := module.Ref("PriceService").(*PriceService)
return ctx.JSON(core.Map{"data": service.Message})
})
return ctrl
}
```
## Contributing
We welcome contributions! Please feel free to submit a Pull Request.
## Support
If you encounter any issues or need help, you can:
- Open an issue in the GitHub repository
- Check our documentation
- Join our community discussions