How to notify the user that a notification has arrived using the websocket gorilla

Viewed 39

How to notify the user that a notification has arrived using the websocket gorilla. the socket route looks like ws://localhost:8080/ws/{id}. id is the id of the user. I have a notification table in a database. There is a user_id field. I would like these users to be notified.

func signal(c *websocket.Conn) {
    eventBytes, err := json.Marshal(<-Chann)
    err = c.WriteMessage(1, eventBytes)
    for {
        if err != nil {
            log.Println("err ", err)
            break
        }
        break
    }
}

func (h *notificationHandler) Connect(w http.ResponseWriter, r *http.Request) {
    id := r.URL.Query().Get("id")  
    c, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Print("upgrade:", err)
        return
    }
    defer c.Close()
    for {
        signal(c)
    }
}

func (h *notificationHandler) CreateNotification(w http.ResponseWriter, r *http.Request) error {
    ...
    go func() {
        Chann <- map[string]string{
            "event": "signal",
        }
    }()
    ...
}

Notification Table has id, text, user_id

the client only needs to send a event signal that a new notification has arrived without text

0 Answers
Related