Issue
This Content is from Stack Overflow. Question asked by Almaz
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
| id | text | user_id |
| — |—— | ——- |
| 1 | Text 1| 1 |
| 2 | Text 2| 4 |
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.