Skip to content

Instantly share code, notes, and snippets.

@yannvery
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save yannvery/93a88417c49156c4aad7 to your computer and use it in GitHub Desktop.

Select an option

Save yannvery/93a88417c49156c4aad7 to your computer and use it in GitHub Desktop.
Implement Faye with Rails 4

Faye & Rails 4

This gist describes how to implement faye with a rails 4 application.

What I want ?

A user receives a live notification when another user login.

Requirements

A rails 4 application with devise for user management

2 gems:

A jquery plugin to notify my users like http://notifyjs.com/

Installation

1 - Gemfile

After gems thin and faye have been added to Gemfile make bundle install

2 - Configure Faye server and start it

Create a faye.ru file and launch you server with rackup faye.ru -s thin -E production

3 - Add faye.js to application.html.erb

Just read the file : application.html.erb

4 - Permit a browser to establish a connection with faye server

Add faye_websocket.js on your app/assets/javascripts folder

5 - Application.js

Load previous file on application.js

6 - FayeNotification Service Object

Add a service object to publish a message to a faye channel without use EventMachine

7 - Finally

Call FayeNotification after a user login with application_controller.rb and its after_sign_in_path_for method

Run

Now you just need to run you faye server with : rackup faye.ru -s thin -E production

# ...
gem 'faye'
gem 'thin'
# ...
# To launch faye server you need to call : rackup faye.ru -s thin -E production
require 'faye'
Faye::WebSocket.load_adapter('thin')
Faye.logger = Logger.new('log/faye.log')
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
run faye_server
....
<head>
<!-- Include faye library to make connection to faye server from browser -->
<%= javascript_include_tag "http://localhost:9292/faye.js" %>
</head>
....
// app/assets/javascripts/faye_websocket.js
// Make a connection to faye server from browser and subscribe to /login channel
//= require notify.min.js
$(function() {
var faye = new Faye.Client('http://localhost:9292/faye');
var subscription = faye.subscribe('/login', function(message) {
$.notify(message, 'info');
});
});
// Load faye_websocket
// ...
//= require faye_websocket
// ...
# app/services/faye_notification.rb
# Service object to publish a message to a faye channel without use EventMachine
class FayeNotification
def initialize(channel:, text:)
@channel = channel
@text = text
end
def publish
uri = URI.parse("http://localhost:9292/faye")
message = {channel: @channel, data: @text}
Net::HTTP.post_form(uri, message: message.to_json)
end
end
class ApplicationController < ActionController::Base
# ...
# Use devise callback to send a notification to faye server
def after_sign_in_path_for(resource)
stored_location_for(resource) ||
if resource.is_a?(User)
FayeNotification.new(channel: '/login', text: "#{current_user.email} is logged").publish
root_url
else
super
end
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment