Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewChannel ¶
NewChannel returns a new typevent.Channel backed by redis Pub/Sub as the underlying event bus.
The channel will emit and subscribe to events of type E. The `name` param is a unique identifier for the event. All channels created with the same `name` must use the same type `E`. The package does intentionally not enforce this constraint, as it would require the use of reflection.
Redis Pub/Sub is used as the underlying event bus. The events emitted on the channel are passed to all channels subscribed on the same `name` on the same redis server, regardless of the DB they're connected to – see https://redis.io/docs/interact/pubsub/#database--scoping.
Example ¶
server, err := miniredis.Run() if err != nil { panic(err) } defer server.Close() type event string // Create a new channel using redis Pub/Sub as the underlying event bus. client := redisclient.NewClient(&redisclient.Options{Addr: server.Addr()}) conf := redis.NewConfig(client) channel := redis.NewChannel[event](conf, "CHANNEL_NAME") // The WaitGroup is necessary to make the example test pass. In a real application, // you would likely just let the handler run until its context is canceled. wg := sync.WaitGroup{} wg.Add(1) // Register a subscriber for the channel. sub, _ := channel.Subscribe(context.Background(), func(ctx context.Context, ev event) error { defer wg.Done() fmt.Printf("subscriber says: %s\n", ev) return nil }) defer sub.Close() // Emit an event on the channel. channel.Emit("Hello World!") wg.Wait() // again, just there to statisfy the example test
Output: subscriber says: Hello World!
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config contains the configuration for a redis channel.
type ConfigOption ¶
type ConfigOption func(*Config)
ConfigOption is a configuration option for the redis channel.
func WithCodec ¶
func WithCodec(codec Codec) ConfigOption
WithCodec sets the codec used to serialize and deserialize events.
func WithKeyPrefix ¶
func WithKeyPrefix(prefix string) ConfigOption
WithKeyPrefix sets the prefix for all redis keys used by the channel.
type JSONCodec ¶
type JSONCodec struct{}
JSONCodec is a codec that uses JSON as the encoding.
type MsgpackCodec ¶
type MsgpackCodec struct{}
MsgpackCodec is a codec that uses msgpack as the encoding.