Question

Is there a simple way to integrate Faye with Sinatra in a single file like this:

# myapp.rb
require 'sinatra'
require 'faye'

get '/' do 
  'Hello world!' 
end

get '/faye_form' do
  erb :form_and_message_list
end

post '/faye_form' do
  erb :form_and_message_list
  # will send messages to faye's channel here.
end

ruby myapp.rb

so that when I run "ruby myapp.rb" I run in a single process Faye and Sinatra servers (they will be on the same port I guess?) ?

or: what's the closest thing to this, with the purpose of integrating Faye in Sinatra in the most "minimalistic" way ?

update: fixed example code, so that it doesn't look like I am interested in having Faye and Sinatra under the same path ;)

Was it helpful?

Solution

Adapted from http://faye.jcoglan.com/ruby.html

# config.ru
require 'sinatra'
require 'faye'
require File.expand_path('../app', __FILE__)

use Faye::RackAdapter, :mount => '/faye', :timeout => 25

run Sinatra::Application

Both Sinatra and Faye are rack apps, and you probably want them running on different paths, like with Faye at /faye. You could also mount faye at / and your Sinatra app somewhere else, although I think you'll need to create a "modular" Sinatra app to do that, and then something like:

run Rack::URLMap.new("/app" => MyApp.new)

In your example, you're sort of trying to have them both under the same path, which I don't think will work.

OTHER TIPS

I have created an example of how Sinatra and Faye can work together simply. You can view it here: https://github.com/eterry1388/sinatra-faye-example

Your config.ru could look like this:

require 'faye'
require 'sinatra'

set :port, 9292
set :faye_client, Faye::Client.new( 'http://localhost:9292/faye' )
set :saved_data, Hash.new( [] )

get '/' do
  # This loads the saved data so if the web page is refreshed
  # or a new client connects, the data persists.
  @saved_data = settings.saved_data
  erb :index
end

post '/' do
  channel = params['channel']
  message = params['message']

  # Send data out to connected clients
  settings.faye_client.publish( channel, message )

  # Save data for future clients
  settings.saved_data[channel] += [message]

  redirect to( '/' )
end

Faye::WebSocket.load_adapter 'thin'
use Faye::RackAdapter, mount: '/faye', timeout: 45, extensions: []
run Sinatra::Application

And your index.erb could look like this:

<script type="text/javascript" src="http://localhost:9292/faye/client.js"></script>

<script type="text/javascript">
  var faye_client = new Faye.Client( 'http://localhost:9292/faye' );

  faye_client.subscribe( '/blue', function( data ) {
    var div = $( 'ul#blue-messages' ).append( '<li>' + data + '</li>' );
  });

  faye_client.subscribe( '/green', function( data ) {
    var div = $( 'ul#green-messages' ).append( '<li>' + data + '</li>' );
  });

  $( document ).ready( function() {
    $( 'button#submit' ).click( function() {
      $.post('/',
      $( 'form#send-message' ).serialize(),
      function() {
        $( 'textarea#message' ).val( '' );
      });
    });
  });
</script>

<h2>Send message</h2>

<form id="send-message">
  <label for="channel">Channel</label>
  <select name="channel" id="channel" class="form-control">
    <option>/blue</option>
    <option>/green</option>
  </select>
  <label for="message">Message</label>
  <textarea name="message" id="message" class="form-control"></textarea>
</form>
<button id="submit" class="btn btn-success">Send</button>

<h2>Blue messages</h2>
<ul id="blue-messages">
  <%
    # The below block is only used for loading historical
    # saved data, not the Faye data!
  %>
  <% @saved_data['/blue'].each do |message| %>
    <li><%= message %></li>
  <% end %>
</ul>

<h2>Green messages</h2>
<ul id="green-messages">
  <%
    # The below block is only used for loading historical
    # saved data, not the Faye data!
  %>
  <% @saved_data['/green'].each do |message| %>
    <li><%= message %></li>
  <% end %>
</ul>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top