简体   繁体   中英

Rails - Faye pushing data

I dont know how to publish some data from rails server, I would like to do similar like in js

var publication = client.publish('channel', {data});

I found someting like that:

engine.publish(message, channels)

but dont know how to use it

At server side, in js.erb file you will have to broadcast message to a channel. ie

<% broadcast "/messages/new" do %>
  $("#chat").append("<%= escape_javascript render(@message) %>");
<% end %>

To broadcast the message/data from controllers. ie

def create
  respond_to do |format|
    format.json do 
      broadcast "channel" do 
         @message.to_json
      end 
    end
  end
end

At client side you will have to include the faye javascript in layout or template and will have to subscribe the channel. ie

<%= javascript_include_tag :defaults, "http://localhost:9292/faye.js" %>

and a dom container in layout or template. ie

<ul id="chat">
  <%= render @messages %>
</ul>

and Subscribe the channel via javascript. ie

$(function() {
  var faye = new Faye.Client('http://localhost:9292/faye');
    faye.subscribe("/messages/new", function(data) {
    eval(data);
  });
});

reference - http://railscasts.com/episodes/260-messaging-with-faye

[SOLVED]

faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
faye_server.get_client.publish('/foo', data )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM