I'm trying to build a simple websocket notification system using rails observers gem and faye. The problem is that I can't publish with faye from the observer.
First I created the observer:
class ScoreObserver < ActiveRecord::Observer
def after_save(score)
@notification = Notification.new(user_id: '2', content: '¡rororscoreeeeeeeeeeeeee!')
if @notification.save
client = Faye::Client.new('http://localhost:9292/faye')
client.publish("/messages/new", {
'title' => 'testitle',
'content' => 'testcontent'
})
end
end
end
As you can see it 'observes' the scores model and insert a record in the notifications table. After that I want it to push a notification with faye using pubilsh.
Then in the client side I run the subscribe script
$(function() {
var faye = new Faye.Client('http://localhost:9292/faye');
faye.subscribe("/messages/new", function(data) {
eval(data);
alert('it works')
});
});
And it doesn't work. The observer is aware of the action and makes the record save correctly but the client doesn't receive any push from the websocket.
I've tested the subscription with a simple broadcast and it works so it seems that is the observer part which is failling. Websockets are up and listening
Do you have any clue of what could be wrong ?
I was looking for an alternative just in case this approach is wrong, and I thought about render a script from the observer and broadcast/publish from a .js file, since all the tuts do it from a script. But I'm having some trouble with this approach too, I can't render a script from the observer in respond_to since the model extends from Observer and not from Base and doesn't have the method.
Thanks
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire