I want to make notification system in my project which is has chat module.To be more specific, when I send message receiver receive notification. I have already made notification system and its working. My problem is that when user open second tab my notification system doesn't work fluently.
There is my code
func GetNotification(w http.ResponseWriter, r *http.Request,id ,timestamp int) {
LastMessageTime := -1
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
beego.Error(err)
return
}
wakeupNotification(id)
for {
select {
case notificationTime := <-MessageNotification[strconv.Itoa(id)] :
if notificationTime > LastMessageTime {
LastMessageTime = notificationTime
//MessageNotification[strconv.Itoa(id)] <-notificationTime
chat_services,err := models.GetChatsNotifications(id,timestamp)
if err != nil{
return
}
not := notification{Status:true,MessageList:chat_services}
conn.WriteJSON(not)
}
}
}
}
func SetNotification(id int,timeStamp int){
go func(){
wakeupNotification(id)
MessageNotification[strconv.Itoa(id)] <- timeStamp
}()
}
func wakeupNotification(id int){
_,ok := MessageNotification[strconv.Itoa(id)]
if !ok {
MessageNotification[strconv.Itoa(id)] = make(chan int)
}
}
func SleptNotification(id int){
wakeupNotification(id)
<-MessageNotification[strconv.Itoa(id)]
}
Also i have controller to trigger
func (c *ChatController) Notification() {
chat.GetNotification(c.Ctx.ResponseWriter,c.Ctx.Request,1,1)
}
func (c *ChatController) DeleteNotification(){
chat.SleptNotification(1)
c.Data["json"] = "readed"
c.ServeJSON();
}
func (c *ChatController) Websocket(){
//odalari olusturup mesajlasma bolumu yapilacak
chat.SetNotification(1,int(time.Now().Unix()))
c.Data["json"] = "test to trigger chan"
c.ServeJSON();
}
My problem is notificationTime := <-MessageNotification[strconv.Itoa(id)]
work one time and don't work again.So code never triggered again in browser second tab.
Do you have any idea ?
Code work fluently. Sorry for English. Thanks for your help.