Last active
August 6, 2021 11:39
-
-
Save percybolmer/6a8a0833cc1cd1cdb48668e1415d9998 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"time" | |
"github.com/go-redis/redis/v8" | |
) | |
func main() { | |
// Create a new Redis Client | |
redisClient := redis.NewClient(&redis.Options{ | |
Addr: "redis:6379", // We connect to host redis, thats what the hostname of the redis service is set to in the docker-compose | |
Password: "superSecret", // The password IF set in the redis Config file | |
DB: 0, | |
}) | |
// Ping the Redis server and check if any errors occured | |
err := redisClient.Ping(context.Background()).Err() | |
if err != nil { | |
// Sleep for 3 seconds and wait for Redis to initialize | |
time.Sleep(3 * time.Second) | |
err := redisClient.Ping(context.Background()).Err() | |
if err != nil { | |
panic(err) | |
} | |
} | |
ctx := context.Background() | |
// Subscribe to the Topic given | |
topic := redisClient.Subscribe(ctx, "new_users") | |
// Get the Channel to use | |
channel := topic.Channel() | |
// Itterate any messages sent on the channel | |
for msg := range channel { | |
u := &User{} | |
// Unmarshal the data into the user | |
err := u.UnmarshalBinary([]byte(msg.Payload)) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(u) | |
} | |
} | |
// User is a struct representing newly registered users | |
type User struct { | |
Username string | |
Email string | |
} | |
// MarshalBinary encodes the struct into a binary blob | |
// Here I cheat and use regular json :) | |
func (u *User) MarshalBinary() ([]byte, error) { | |
return json.Marshal(u) | |
} | |
// UnmarshalBinary decodes the struct into a User | |
func (u *User) UnmarshalBinary(data []byte) error { | |
if err := json.Unmarshal(data, u); err != nil { | |
return err | |
} | |
return nil | |
} | |
func (u *User) String() string { | |
return "User: " + u.Username + " registered with Email: " + u.Email | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment