ngauthier / tubesock

Websocket interface on Rack Hijack w/ Rails support
MIT License
620 stars 43 forks source link

In the chrome console I get WebSocket connection failed and from rails console I get Tubesock::HijackNotAvailable with rails-4.1 #18

Closed boy-jer closed 10 years ago

boy-jer commented 10 years ago

Just following your simple chat example. And when I naviage to my index page or to localhost:3000/chat, I get this error in the console:

 WebSocket connection to 'ws://app-dev-32586.euw1.actionbox.io/chat' failed: Unexpected response code: 200   

please note that since I use nitous.io, my localhost is app-dev-32586.euw1.actionbox.io

When I submit a form, it throws in chrome console:

  WebSocket is already in CLOSING or CLOSED state.                                                              

my gemfile:

gem "rails", "~> 4.1.0.beta1"
gem "puma", "~> 2.7.1"
gem "tubesock", "~> 0.2.2"

My route:

 get 'index' => 'chat#index'
 get "chat/chat" => 'chat#chat'

This is my controller

 class ChatController < ApplicationController
  include Tubesock::Hijack

  def chat
     hijack do |tubesock|
         tubesock.onopen do
            tubesock.send_data "Hello, friend"
         end

         tubesock.onmessage do |data|
            tubesock.send_data message: "You said: #{data}"
         end
      end
   end  
end

The js file and the erb file are exactly same as this in your example:

 https://github.com/ngauthier/sock-chat/blob/master/app/views/chat/index.html.erb

  https://github.com/ngauthier/sock-chat/blob/master/app/assets/javascripts/chat.js.coffee

I also change the url passed to websocket to point to "/chat/chat" but it changed nothing.

 socket = new WebSocket("ws://" + location.host + "/chat/chat");

In the rails console, when I do:

 >>   ChatController.new.chat 

The stack trace:

      Tubesock::HijackNotAvailable: Tubesock::HijackNotAvailable                                                                                        
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/tubesock0.2.2/lib/tubesock.rb:31:in `hijack'                                            
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/tubesock0.2.2/lib/tubesock/hijack.rb:30:in `hijack'                                     
        from /home/action/workspace/smb/app/controllers/chat_controller.rb:5:in `chat'                                                            
        from (irb):3                                                                                                                              
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.1.0.beta1/lib/rails/commands/console.rb:90:in `start'                         
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.1.0.beta1/lib/rails/commands/console.rb:9:in `start'                          
        from /home/action/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.1.0.beta1/lib/rails/commands/commands_tasks.rb:69:in `console'          
        from bin/rails:4:in `require'                                                                                                             
        from bin/rails:4:in `<main>'  

Any suggestions. Thanks

ngauthier commented 10 years ago

I'm not sure what nitrous is using to proxy the server, it may not support websockets. Can you check with them? HijackNotAvailable means you probably aren't running puma, so check that nitrous is running your app w/ puma too.

-Nick

boy-jer commented 10 years ago

Thanks Nick for the response and for writing the gem. I go working and I don't know what I changed. As I just kept trying different things that came to my head.

For the url passed to websocket, this is the one that worked, and before it failed so obviously I changed something else that made it work. That something is what I can't immediately place my hand on:

  socket = new WebSocket("ws://" + window.location.host + "/chat/chat");

Thanks again.

boy-jer commented 10 years ago

[1]. Ooops before I close it, I just want to ask if the controller action used for calling hijack do |tubesock|* can be a create action, so we make a post request instead of get. All the examples I have seen use a get request.

  class ChatController < ApplicationController
     include Tubesock::Hijack
     def create
       hijack do |tubesock|
           tubesock.onopen do
                tubesock.send_data "Hello, friend"
            end
       end    
    end  
  end

Is such usage appropriate.

[2]. Secondly supposed I have a ChatController with its model and another SupportTicketsController and its model, can I add * include Tubesock::Hijack* and create a new \ hijack do |tubesock|. I am asking because I tried this and I am now getting No data received* even for the ChatController* that previously worked.

Thanks

ngauthier commented 10 years ago

I think WS requests must be gets. I dunno. I am not sure what situation that would make sense in.

Tubesock should work in multiple controllers. I don't know what the problem is.

boy-jer commented 10 years ago

ok thanks.

nengxu commented 10 years ago

I just tried the chat demo application with tubesock (0.2.2), Rails 4.1.0.beta1, jubilee (1.1.0-java), JRuby 1.7.9. Got similar errors:

In Chrome console:

WebSocket connection to 'ws://10.10.22.116:3000/chat' failed: Error during WebSocket handshake: Unexpected response code: 500 

In Rails log:

Started GET "/chat" for 10.10.23.94 at 2014-01-24 10:58:25 -0800
Processing by ChatController#chat as HTML
Completed 500 Internal Server Error in 7ms

Tubesock::HijackNotAvailable (Tubesock::HijackNotAvailable):
  app/controllers/chat_controller.rb:5:in `chat'

Note that Rails treated the request as HTML. Is this the normal way?

@ngauthier Could you please reopen this issue? Or should I create a new one? Thanks.

ngauthier commented 10 years ago

Hi @nengxu. What web server are you using to serve the app? It must support Rack Hijack for Tubesock Hijack to work.

The only one I've had consistent results with is Puma. Are you using puma from jruby? I also haven't tried Tubesock on JRuby.

nengxu commented 10 years ago

As mentioned above, I'm using Jubilee, not Puma.

I just tried Puma (2.7.1) on JRuby, it worked. However, whenever I enter something in the input field, all clients will receive the 'something' twice. I will open a new issue for this.

Thanks.

ngauthier commented 10 years ago

OK. It's probably a difference in threading between mri and jruby, but we can check it out in a separate issue. Also, using jubilee should be a separate issue too :-)

Thanks!

nengxu commented 10 years ago

Confirmed that Jubilee did not support hijack yet:

https://github.com/isaiah/jubilee/issues/4#issuecomment-33410647

ngauthier commented 10 years ago

That solves that! :D

On Mon, Jan 27, 2014 at 2:23 PM, nengxu notifications@github.com wrote:

Confirmed that Jubilee did not support hijack yet:

isaiah/jubilee#4 (comment)https://github.com/isaiah/jubilee/issues/4#issuecomment-33410647

— Reply to this email directly or view it on GitHubhttps://github.com/ngauthier/tubesock/issues/18#issuecomment-33410756 .