You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
621 B
35 lines
621 B
7 months ago
|
package redispubsub
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/redis/go-redis/v9"
|
||
|
)
|
||
|
|
||
|
var ctx = context.Background()
|
||
|
|
||
|
type Subscriber struct {
|
||
|
client redis.UniversalClient
|
||
|
channel string
|
||
|
}
|
||
|
|
||
|
func NewSubscriber(client redis.UniversalClient, channel string) *Subscriber {
|
||
|
return &Subscriber{client: client, channel: channel}
|
||
|
}
|
||
|
|
||
|
func (s *Subscriber) OnMessage(ctx context.Context, callback func(string)) error {
|
||
|
messageChannel := s.client.Subscribe(ctx, s.channel).Channel()
|
||
|
|
||
|
go func() {
|
||
|
for {
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
return
|
||
|
case msg := <-messageChannel:
|
||
|
callback(msg.Payload)
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
return nil
|
||
|
}
|