rails app as a client of faye?

72 views
Skip to first unread message

hau

unread,
Oct 20, 2011, 9:18:25 AM10/20/11
to Faye users
I was wondering if it's possible to make a rails app subscribe to some
faye channel. Messages on this channel will contain information that
I would use to operate on rails models. I was thinking of using a
faye server extension that will publish a message to this rails
channel when it sees a /meta/subscribe message. Then the subscriber
inside of rails will use some data model to record this subscription.
I was thinking of starting a faye client which subscribes to this
channel when rails app starts up. I would make it an initializer,
e.g.

in config/initializers/faye_client.rb:
$client = Faye::Client.new('http://localhost:9292/faye')
client.subscribe('/railsapp') do |message|
RailsAppModel.create(message.ext.user_id)
end

In faye's documentation, it says that a faye client needs to run
inside of eventmachine. That means I cannot really do what I wrote
above, but I would need to put the code inside of
EM.run do
#code goes here
end

But then the initializer will go into a loop and never returns, in
which case the rails app will not run. Could someone help with this
problem of making a rails app listen on faye channels?

Actually, I'm not even sure if I can publish messages inside of a faye
server extension. My problem really is how should a faye server
communicate with a rails application.

Thank you for your help.

radhames brito

unread,
Oct 20, 2011, 11:58:00 AM10/20/11
to Faye users
Here is now i am doing it.

i create a class and set this function

def self.set_connection
ensure_reactor_running
@@client ||= Faye::Client.new("http://#{FAYE_SERVER}:9292/faye",
{timeout: 20})
@@client.add_extension(ClientAuth.new)
@@client
end

private
def self.ensure_reactor_running
Thread.new { EM.run } unless EM.reactor_running?
sleep 0.1 until EM.reactor_running?
end



It works but for some strange reason the process end every once in a
while , and i have no idea how to debug it since there is no output.

One thing to note is that faye will be running in a different server
(thin) and you are communicating to it via the protocol, like any
other client. Conclusion, the Faye server never runs inside your rails
app, the client does.

radhames brito

unread,
Oct 25, 2011, 9:56:33 AM10/25/11
to Faye users
On Thu, Oct 20, 2011 at 11:58 AM, radhames brito <rbr...@gmail.com> wrote:
Here is now i am doing it.

i create a class and set this function

def self.set_connection
   ensure_reactor_running
   @@client ||= Faye::Client.new("http://#{FAYE_SERVER}:9292/faye",
{timeout: 20})
   @@client.add_extension(ClientAuth.new)
   @@client
 end

private
 def self.ensure_reactor_running
       Thread.new { EM.run } unless EM.reactor_running?
       sleep 0.1 until EM.reactor_running?
 end


I was aggregating extensions  :(
Reply all
Reply to author
Forward
0 new messages