-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.go
44 lines (35 loc) · 877 Bytes
/
simple.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
package main
import (
"fmt"
"github.com/RedisMPX/go-mpx"
"github.com/gomodule/redigo/redis"
"time"
)
func main() {
connBuilder := func() (redis.Conn, error) {
return redis.Dial("tcp", ":6379")
}
// Create a Multiplexer
multiplexer := mpx.New(connBuilder)
// Define a onMessage callback
onMessage := func(ch string, msg []byte) {
fmt.Printf("channel: [%v] msg: [%v]\n", ch, string(msg))
}
// Create a Subscription
sub := multiplexer.NewChannelSubscription(onMessage, nil, nil)
// Add a Redis Pub/Sub channel to the Subscription
sub.Add("mychannel")
// Create a second connection to Redis
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
panic(err)
}
// Publish Messages over Redis Pub/Sub forever.
for {
time.Sleep(2 * time.Second)
_, err := conn.Do("PUBLISH", "mychannel", "Hello World!")
if err != nil {
panic(err)
}
}
}