Question

Im trying to build a simple faye realtime based notification system so I can execute a certain javascript on certain actions.The idea is relatively simple though Im having problems implementing it, not sure wich road to take on this after reading the documentation of faye.

Current thoughts are

  • One unique faye channel per logged-in user, so you can push a action (popup, set text) to a certain user only
  • One div in my app layout that I can write text to
  • One div in my app that holds layout for a popup

Now I've seen the railscast tutorial about faye but ryan works from a controller/action method create. I don't want to insert stuff in the db, just call the JS function from anywhere ( building an application helper would be a good idea I think ), I would just want to do something like "execute javascript 'set_text'" and execute javascript 'show_popup'

What would be the best way to build such functionality with Faye, Basically I only have to

  • Execute a javascript function on a certain Faye channel

To accomplish the popup and text message. A bit lost on this anyone can point me in the right direction or maybe have build such functionality already? thx in advanche!

Was it helpful?

Solution

On the server side, you can just do (this needs eventmachine):

client = Faye::Client.new('http://localhost:9292/faye')
client.publish('/notifications/1', 'alert(1);')

Or via HTTP:

message = {:channel => '/notifications/1', :data => 'alert(1);'}
uri = URI.parse("http://localhost:9292/faye")
Net::HTTP.post_form(uri, :message => message.to_json)

Then on the client side, you can then do anything with the data.

var client = new Faye.Client('http://localhost:9292/faye');
client.subscribe('/notifications/1', function(data) {
  eval(data);
});

Just replace alert(1); with whatever JS you want to execute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top