faye ruby client is not working
I finally used HTTP not the Ruby Faye client as described in railscasts episode 260.
require 'net/http'
message = {:channel => '/faye/new_chats', :data => self.text, :ext => {:auth_token => FAYE_TOKEN}}
uri = URI.parse("http://localhost:9292/faye")
Net::HTTP.post_form(uri, :message => message.to_json)
It solves my problem.
NOTE: This solution only works with HTTP but not with HTTPS. If any one find a solution for HTTPS plz update me.
Faye Websocket Ruby not working as expected
Okay, After a lot of searching and testing.. I found that the issue was with not setting a ping. during websocket initialization in the server.
Change this
ws = Faye::WebSocket.new(env, nil) # {ping: KEEPALIVE_TIME }
to
ws = Faye::WebSocket.new(env, nil, {ping: KEEPALIVE_TIME })
My KEEPALIVE_TIME is 0.5 because I am making a stock application where rates change very quickly. You can keep it per your needs.
Messaging with Faye : ReferenceError: Faye is not defined
I would suggest you to use your actual ip
instead of localhost
in Faye.Client
initialization in production environment(of course use request.host
helper for this):
var faye = new Faye.Client("http://#{ request.host }:9292/faye");
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
Faye does not publish when using a browser on another computer in the network
To resolve the issue I replaced all instances of "localhost" with the address of the server on which Faye is running. This includes for subscribing clients to channels as well.
Hope it helps,
Cheers!
Faye, websocket connection problems: can't establish connection with ws://localhost:9292/faye
Apparently, you need at least version 0.7 to support websockets. http://blog.jcoglan.com/2011/11/28/announcing-faye-websocket-a-standards-compliant-websocket-library/ This meant updating rubygems, for me, and re-running bundle install. I also needed to change my .ru file to include the following:
Faye::WebSocket.load_adapter('thin')
Related Topics
How to Get All Arguments of a Function as Single Object Inside That Function
How to Improve Performance of Ngrepeat Over a Huge Dataset (Angular.Js)
Ckeditor 4: Uncaught Typeerror: Cannot Read Property 'Langentries' of Null
Firebase.Database Is Not a Function
Splitting a Js Array into N Arrays
Maintain Model of Scope When Changing Between Views in Angularjs
How to Save HTML Page as PDF Using JavaScript or Jquery
How to Read a Httponly Cookie Using JavaScript
React - Changing an Uncontrolled Input
Converting JSON Format to CSV to Upload Data Table in R to Produce D3 Bubble Chart
How to Detect Scroll Position of Page Using Jquery
How to Set Header and Options in Axios
Performance of Key Lookup in JavaScript Object