I am trying to setup message notifications in an Angular/Rails app.
When the user logs in, I want to open a SSE connection which will subscribe to a Redis stream and pu
The problem was that I was putting the function that creates an EventSource
in my get
attribute, instead of putting the actual EventSource
object. A few changes makes it work:
.factory('StreamHandler', function(CookieHandler, MessageStream){
var StreamHandler = {
set: function(){
var user
user = CookieHandler.get();
var source = new EventSource('/api/v1/messages/count?id='+user.id)
MessageStream.get = source
},
get: function(){
var source = MessageStream.get
source.onmessage = function(event) {
console.log(event)
}
source.onerror = function(error) {
source.close()
}
},
kill: function(){
var source = MessageStream.get
source.close();
}
}
return StreamHandler
})