How can I push to Faye Server from Rails Controller?
I did't use Event Machine but I use Fay-web Socket in rails , I am using thin web-server for my application to show notification.
First you add this line into you Gemfile
gem 'faye'
gem 'thin'
Now ! run bundle install command for install gem and it's
dependency.Create a faye.ru file and add given line (a rackup file for run
Faye server ).
require 'rubygems'
require 'thin'
require 'faye'
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
run faye_server
Now add line to your application.erb file
<%= javascript_include_tag 'application', "http://localhost:9292/faye.js", 'data-turbolinks-track' => true %>
Create a method with name broadcast or any name which is
suitable for you inwebsoket.rb
(first create websoket.rb file
insideconfig/initializers
) .
module Websocket
def broadcast(channel, msg)
message = {:channel => channel, :data => msg}
uri = URI.parse("http://localhost:9292/faye")
Net::HTTP.post_form(uri, :message => message.to_json)
end
end
Now use this method inside your model or controller where you want.
In my case I am using this inside the **Notification.rb to sending notification.**
Example
after_create :send_notificaton
def send_notification
broadcast("/users/#{user.id}", {username: "#{user.full_name }", msg: "Hello you are invited for project--| #{project.name} | please check your mail"})
end
For subscriber
<div id="websocket" style="background-color: #999999;">
</div>
<script>
$(function () {
var faye = new Faye.Client('http://localhost:9292/faye');
faye.subscribe('/users/<%= current_user.id %>', function (data) {
$('#websocket').text(data.username + ": " + data.msg);
});
});
</script>
Now ! Run your faye.ru file using terminal
rackup faye.ru -s thin -E prodcution
For details
Faye websocket
Rails - Faye pushing data
[SOLVED]
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
faye_server.get_client.publish('/foo', data )
How can I have a server side faye client running within my rails app?
I have solved this now by using the 'faye-rails' gem and included this into my application.rb file:
config.middleware.delete Rack::Lock
config.middleware.use FayeRails::Middleware, mount: '/faye', :timeout => 25 do
class RealTime < FayeRails::Controller
channel '**' do
monitor :unsubscribe do
if channel != '/messages' && channel != '/disconnect'
user = channel[6..-1]
@members = Members.where("username = '#{user}'")
data = {:channels => @members, :user => user}
RealTime.publish('/disconnect', data)
@members.destroy_all
end
end
end
end
add_extension(RealTime.new)
end
I also have a own channel for each user which I use to get the username and delete data from the database.
Checking if the Faye server exists before running it for my Rails app
You can use the DaemonController library. It will enable you to auto-start services with your Rails app, starting them if they aren't already started.
Related Topics
Chaining Methods Using Symbol#To_Proc Shorthand in Ruby
How to Use Params with Slashes with Sinatra
No Such File or Directory - Git Ls-Files -- Windows
How Is a Local Variable Created Even When If Condition Evaluates to False in Ruby
How to Dump Strings in Yaml Using Literal Scalar Style
How to Track the Execution Process of Ruby Program
Decrypting Salted Aes File Generated on Command Line with Ruby
Throw Exception When Re-Assigning a Constant in Ruby
Language in a Sandbox in Rails
Rails 3.2 'Link_To' (In Email) with 'Method: :Put' Still Producing Get Request
How to Match Full Words and Not Substrings in Ruby
When Did "Assigned But Unused" Become a Warning for Ruby
Ruby on Rails Display Half a Star for a Decimal Rating, E.G. 4.5
Why Does Ruby's String#To_I Sometimes Return 0 When the String Contains a Number