/
publisher.go
49 lines (39 loc) · 1012 Bytes
/
publisher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package webapp
import (
"encoding/json"
goredis "github.com/go-redis/redis/v8"
"github.com/authgear/authgear-server/pkg/auth/webapp"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/infra/redis/appredis"
"github.com/authgear/authgear-server/pkg/util/pubsub"
)
type Publisher struct {
AppID config.AppID
RedisHandle *appredis.Handle
Publisher *pubsub.Publisher
}
func NewPublisher(appID config.AppID, handle *appredis.Handle) *Publisher {
p := &Publisher{
AppID: appID,
RedisHandle: handle,
}
p.Publisher = &pubsub.Publisher{
RedisPool: p,
}
return p
}
func (p *Publisher) Get() *goredis.Client {
return p.RedisHandle.Client()
}
func (p *Publisher) Publish(s *webapp.Session, msg *WebsocketMessage) error {
channelName := WebsocketChannelName(string(p.AppID), s.ID)
b, err := json.Marshal(msg)
if err != nil {
return err
}
err = p.Publisher.Publish(channelName, b)
if err != nil {
return err
}
return nil
}