Skip to content

Commit

Permalink
feat: added redis
Browse files Browse the repository at this point in the history
  • Loading branch information
Bikram-ghuku committed Jul 4, 2024
1 parent 56513df commit 03bf688
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion socketServer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"

"github.com/go-redis/redis"
"github.com/gorilla/websocket"
"github.com/joho/godotenv"
)
Expand All @@ -18,6 +19,10 @@ var upgrader = websocket.Upgrader{
},
}

var redisClient = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})

func Handler(res http.ResponseWriter, req *http.Request) {
c, err := upgrader.Upgrade(res, req, nil)
if err != nil {
Expand All @@ -31,7 +36,10 @@ func Handler(res http.ResponseWriter, req *http.Request) {
log.Println(err.Error())
break
}
log.Printf("Received: %s", msg)
//log.Printf("Received: %s", msg)
if err := redisClient.Publish("send-user-data", msg).Err(); err != nil {
panic(err)
}
err = c.WriteMessage(mt, msg)
if err != nil {
log.Println("write: ", err)
Expand All @@ -42,8 +50,14 @@ func Handler(res http.ResponseWriter, req *http.Request) {

func main() {
godotenv.Load()
if err := redisClient.Ping().Err(); err != nil {
panic(err.Error())
}

http.HandleFunc("/", Handler)
port := os.Getenv("PORT")
log.Printf("Serving at localhost:%s", port)

log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", port), nil))

}

0 comments on commit 03bf688

Please sign in to comment.