Friday 26 June 2015

Faye Web Socket in Rails

Faye is  general-purpose web-socket it's also provide 2-way communication for client side ( user ) or server side. It is very use-full for sending notification on a given condition without reloading the whole page in browser and also help full for showing progress bar on run time server response simple way to implement this gem into your rails application.
Faye is working on multiple web server like

I am using thin web-server for my application to show notification.

1. First you add this line into you Gemfile 
--------------------------------------------------------

gem 'faye'
gem 'thin' 

----------------------------------------
2. Now ! run bundle install   command for install gem and it's dependency.

3. 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


4. Now add line to your application.erb file

<%= javascript_include_tag 'application', "http://localhost:9292/faye.js", 'data-turbolinks-track' => true %>


Now ! adding a method with name broadcast or any name which is subtable for you in websoket.rb (first create websoket.rb file inside initializers ) .

#======confi/initializers/websoket.rb


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



Note: If you want to use this method as a helper method then you add this inside application_helper.rb for view.(I include this method into model for sending notification as per my business logic ) for more detail watch RailsCast 260.

Inside this method channel is use for sending messages on a particular URL and MSG is used for send messages on that particular channel.

example :

#===============include Websocket
 
 
 
Inside Notification.rb model
def send_invitation_notification
   broadcast("/users/#{user.id}", {username: "#{user.full_name }", msg: "Hello you are invited for project--| #{project.name} | please check your mail"})
end


I include Web-socket for access broadcast method in model. Now on browser when we call this method using console then it will get notification but first you have to create a view for this notification and a subscriber inside the view using Faye.Cilent('http://localhost:9292/faye').


Example:


<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>

5. Now ! Run your faye.ru file using terminal


rackup faye.ru -s thin -E prodcution




6. Open User show page something like http://localhost:3000/users/1

7. Now ! send notification using terminal



u = Notification.first
u.broadcast("/projects/16", {username: "your name ",msg: "Hello you did it "})



For more detail about faye gem see it's official website - http://faye.jcoglan.com/ruby.html


No comments:

Salesforce CRM vs. Zoho: A Comparative Analysis

Introduction: Selecting the right customer relationship management (CRM) software is crucial for businesses seeking to streamline their sal...