Search code examples
ruby-on-rails-4websocketopenshiftruby-2.0faye

Openshift: Getting error during WebSocket handshake using faye


I have deployed an app on openshift and I want to implement a chat app using web sockets, so I used faye. It is working on local but I am getting a handshake error on the server when deployed to openshift. This is my config.ru file:

require ::File.expand_path('../config/environment',  __FILE__)
require 'faye'
use Faye::RackAdapter, :mount => '/faye', :timeout => 25
run Rails.application

When I tried to run https://myapp.rhcloud.com/faye.js it was working and also my app functionality is working fine. But I am getting this error:

WebSocket connection to 'wss://friendsbook-cisinlabs.rhcloud.com/faye' failed

application.js

$(function(){
  var faye = new Faye.Client('https://myapp.rhcloud.com/faye');
  faye.subscribe("/messages/new", function(data) {
    eval(data);
  });
});

application.html.erb

  <%= javascript_include_tag '//myapp.rhcloud.com/faye.js' %>

message_helper.rb

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

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

messages/create.js.erb

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

messages/_message.html.erb

<li>
  <span class="created_at"><%= message.created_at.strftime("%H:%M") %></span>
  <%= message.content %>
</li>

I refereed to this railscast episode from Ryan Bates: http://railscasts.com/episodes/260-messaging-with-faye

Also I referred to many other links but nothing helped.

Edit

I have added this code in .openshift/action_hooks/post_start_ruby

nohup bundle exec rackup config.ru -s thin -E production -o $OPENSHIFT_INTERNAL_IP -p 8443 > $OPENSHIFT_HOMEDIR/diy-0.1/logs/server.log 2>&1 &

Now I am getting error:

ReferenceError: Faye is not defined

This is how I defined faye in application.js

var faye = new Faye.Client('https://friendsbook-cisinlabs.rhcloud.com:8443/faye

Probably it doesn't got started on port 8443, how should that be done.


Solution

  • If you want to use wss for your connection, you need to use port 8443, instead of port 443 (which is the default)

    wss://friendsbook-cisinlabs.rhcloud.com:8443/faye