I am using faye in my Rails 2.1 application. And after testing and fixing many things, the faye ruby client does not work.
This is my server code.
require 'faye' server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45) EM.run { thin = Rack::Handler.get('thin') thin.run(server, :Port => 9292) server.bind(:subscribe) do |client_id, channel| puts "[ SUBSCRIBE] #{client_id} -> #{channel}" end server.bind(:unsubscribe) do |client_id, channel| puts "[UNSUBSCRIBE] #{client_id} -> #{channel}" end server.bind(:disconnect) do |client_id| puts "[ DISCONNECT] #{client_id}" end }
This is my client side JS code.
<script type="text/javascript"> var client = new Faye.Client('http://localhost:9292/faye'); client.subscribe("/faye/new_chats", function(data) { console.log(data); }); </script>
This is the ruby ​​client code.
EM.run do client = Faye::Client.new('http://localhost:9292/faye') publication = client.publish("/faye/new_chats", { "user" => "ruby-logger", "message" => "Got your message!" }) publication.callback do puts "[PUBLISH SUCCEEDED]" end publication.errback do |error| puts "[PUBLISH FAILED] #{error.inspect}" end end
Server, JS is working fine. But Ruby client code does not work. If I write it without EM , it will show me an error, which is Event Machine not initialized . If I write it in EM , it works, but causes a ruby ​​process. If I put EM.stop at the end of the client code, it will execute but not post a message.
How can I solve this problem?
source share