2

I am trying to implement a chat feature in my rails app deployed on heroku . For this I have created two rails apps one my original app and the other to acts as faye server. I have given the faye app url in my original app to create faye server object.

Following is what I have added in my original app:

Ruby version: 2.0.0

gemfile

gem 'thin'

gem 'faye'

gem 'rails', '4.1.5'

Application.rb

 <%= javascript_include_tag 'http://xxx.herokuapp.com/faye.js' %>

Application.js

$(function(){
    $(document).foundation();
  var faye = new Faye.Client('http://xxx.herokuapp.com/faye');
  faye.subscribe("/messages/new", function(data) {
    eval(data);
  });
});

Message Helper

def broadcast(channel, &block)
   message = {:channel => channel, :data => capture(&block)}
   uri = URI.parse("http://xxx.herokuapp.com/faye")
   Net::HTTP.post_form(uri, :message => message.to_json)
end

Message Controller

class MessagesController < ApplicationController

  def index
    @messages = Message.all
  end

  def create
    @message = Message.create!(message_params)
  end

  private
    def message_params
      params.require(:message).permit(:content)
    end
end

My View:

messages/create.js.erb

<% broadcast "/messages/new" do %>
    $("#chat").append("<%= escape_javascript render(@message) %>");
<% end %>
$("#new_message")[0].reset();

/messages/index.html.erb

<ul id="chat">
   <%= render @messages %>
</ul>

<%= form_for Message.new, :remote => true do |f| %>
   <%= f.text_field :content %>
   <%= f.submit "Send" %>
<% end %>

Follwing is what I added in my faye server app:

faye.ru

require 'faye'

Faye::WebSocket.load_adapter('thin')
app = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)

run app

Procfile

web:  bundle exec thin -p $PORT -e $RACK_ENV -R faye.ru start

I am getting a 500 internal sever error whenever I try to send a message. The code is working fine on local. Also I am getting Error during WebSocket handshake: Unexpected response code: 404 in chrome. I tried enabling cors but that did not helped either.

Note: Below are some reference links which are not working for me

  1. Using rails sync gem with Faye and Thin in production mode on Heroku

  2. https://blog.heroku.com/archives/2013/10/8/websockets-public-beta

    This is not working heroku labs:enable websockets

I am new for heroku and faye.

Community
  • 1
  • 1
Sourabh Upadhyay
  • 1,034
  • 11
  • 31

0 Answers0