Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web sockets on 0.8 release #48

Closed
glennfu opened this issue Aug 8, 2016 · 27 comments · Fixed by #143
Closed

Web sockets on 0.8 release #48

glennfu opened this issue Aug 8, 2016 · 27 comments · Fixed by #143

Comments

@glennfu
Copy link

glennfu commented Aug 8, 2016

I've followed the setup as best I can here:

development.rb:

config.app_domain = 'mygame.dev'
config.web_socket_server_url = "ws://#{config.app_domain}/cable" 
config.action_cable.allowed_request_origins = /(\.dev$)|^localhost$/

I load the page and see after the page loads...

Started GET "/cable" for 127.0.0.1 at 2016-08-08 14:23:50 -0500
  ActiveRecord::SchemaMigration Load (4.6ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Started GET "/cable/"[non-WebSocket] for 127.0.0.1 at 2016-08-08 14:23:50 -0500
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: , HTTP_UPGRADE: )
Finished "/cable/"[non-WebSocket] for 127.0.0.1 at 2016-08-08 14:23:50 -0500
@evanphx
Copy link
Member

evanphx commented Aug 8, 2016

@glennfu Lot's of questions:

  • Did you mount ActionCable at /cable in your routes?
  • What about if you set config.action_cable.disable_request_forgery_protection = true?
  • Did you change app/assets/javascripts/channels/index.coffee to have App.cable = ActionCable.createConsumer("/cable")?
  • Try not setting web_socket_server_url at all. Also, I see it being called action_cable.url.

@glennfu
Copy link
Author

glennfu commented Aug 8, 2016

  1. Yes - This setup already works in Production, and works if I call rails s in a separate iTerm window and point config.web_socket_server_url to the IP of that running instance.
  2. I tried that, no apparent change.
  3. I tried changing ActionCable.createConsumer() to ActionCable.createConsumer("/cable") but there's no change in the logs/errors
  4. Not setting web_socket_server_url didn't seem to change anything either. The logs all still reference "/cable" as I guess this is the default. I think you're right that action_cable.url is the new preferred way to set but since "/cable" is already the default, setting it here isn't changing anything for my setup.

Also In the FireBug console I see this:

Firefox can’t establish a connection to the server at ws://stinkergame.dev/cable.
this.webSocket = new WebSocket(this.consumer.url, protocols);

The connection to ws://stinkergame.dev/cable was interrupted while the page was loading.
this.webSocket = new WebSocket(this.consumer.url, protocols);

@evanphx
Copy link
Member

evanphx commented Aug 8, 2016

Check if spring has left a rails process running in the background and kill it if so. I had this problem at one point where configuration wasn't taking because of that.

@evanphx
Copy link
Member

evanphx commented Aug 8, 2016

Here is the sample app that I used to test websockets: https://github.com/puma/actioncable-examples

@glennfu
Copy link
Author

glennfu commented Aug 8, 2016

I don't believe I've been using spring, but just in case, I ran spring stop. I normally restart the server with touch tmp/restart.txt && tail -f log/development.log

@navied
Copy link

navied commented Aug 9, 2016

I was having issues with the new release as well but I had to do a puma-dev --uninstall then brew uninstall, after that I reinstalled it thru brew and it started working. A simple brew upgrade did not take effect the new changes, even after a system restart as well.

@evanphx
Copy link
Member

evanphx commented Aug 9, 2016

@navied Yes, that's true and I realize now that's going to be causing a problem. @glennfu can you please do puma-dev -uninstall, then puma-dev -install and try websockets again?

@glennfu
Copy link
Author

glennfu commented Aug 9, 2016

OK I did the following:

puma-dev -uninstall
brew install puma/puma/puma-dev
touch tmp/restart.txt && tail -f log/development.log

No perceived difference. Then I restarted my laptop and tried again. Now I'm actually getting a difference. Unfortunately it's still not working. Now the Firebug log looks like:

"NetworkError: 404 Not Found - http://mygame.dev/cable"
Firefox can’t establish a connection to the server at ws://mygame.dev/cable.
this.webSocket = new WebSocket(this.consumer.url, protocols);

and in Rails log:

Started GET "/cable" for 127.0.0.1 at 2016-08-09 11:05:59 -0500
Started GET "/cable/"[non-WebSocket] for 127.0.0.1 at 2016-08-09 11:05:59 -0500
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: , HTTP_UPGRADE: )
Finished "/cable/"[non-WebSocket] for 127.0.0.1 at 2016-08-09 11:05:59 -0500

These both spam together in unison frequently. Under puma-dev 0.7, I'd only get 1 Rails log request, and tons of Firebug spam about "interrupted".

Here's the puma-dev.log:

→ tail -f ~/Library/Logs/puma-dev.log
→ tail -f ~/Library/Logs/puma-dev.log
mygame[1700]: stty: stdin isn't a terminal
mygame[1700]: stty: stdin isn't a terminal
mygame[1700]: RVM loading: /Users/Glenn/.rvm/environments/ruby-2.3.0
mygame[1700]: Puma starting in single mode...
mygame[1700]: * Version 3.0.2 (ruby 2.3.0-p0), codename: Plethora of Penguin Pinatas
mygame[1700]: * Min threads: 0, max threads: 5
mygame[1700]: * Environment: development
mygame[1700]: * Listening on unix:/Users/Glenn/.puma-dev/mygame/tmp/puma-dev-330.sock
mygame[1700]: Use Ctrl-C to stop
! App 'mygame' booted

@glennfu
Copy link
Author

glennfu commented Aug 9, 2016

Just for some extra context, skipping puma-dev I can do rails s and change config.action_cable.url to "ws://localhost:3000/cable"

Then in the Rails log I see:

Started GET "/cable" for ::1 at 2016-08-09 11:13:36 -0500
Started GET "/cable/" [WebSocket] for ::1 at 2016-08-09 11:13:36 -0500
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)

@evanphx
Copy link
Member

evanphx commented Aug 10, 2016

Just to be sure, can you update puma to the last version as well? I don't think that will fix it but worth it to try.

Also, you don't list doing puma-dev -install in your initial steps, I assume you did do that but forgot to list it?

What version of rails? I think I need to try and replicate the issue locally which I'm not able to do right now. Would you be wiling to try the actioncable-examples repo I linked before and see if it works for you?

@glennfu
Copy link
Author

glennfu commented Aug 10, 2016

Oh sorry, yeah, here's what I actually did:

puma-dev -uninstall
brew install puma/puma/puma-dev
sudo puma-dev -setup
puma-dev -install
touch tmp/restart.txt && tail -f log/development.log
restart computer
touch tmp/restart.txt && tail -f log/development.log

I'll take a look at actioncable-examples later and see if I can reproduce my situation.

@glennfu
Copy link
Author

glennfu commented Aug 10, 2016

OK, I checked out actioncable-examples. It runs ActionCable on a separate server instance, which is not the default setup, and not the situation that I've described here. To get it looking right, I changed development.rb with:

-  config.action_cable.url = 'ws://localhost:28080'
+  config.action_cable.url = "ws://localhost:3000/cable"
+  config.action_cable.allowed_request_origins = ["http://localhost:3000"]

and routes.rb with:

+  mount ActionCable.server => '/cable'

With this change, rails s fires up a server accessible at "http://localhost:3000" and I can go there and test that ActionCable works just fine. The server log shows:

Started GET "/cable" for ::1 at 2016-08-10 12:23:04 -0500
Started GET "/cable/" [WebSocket] for ::1 at 2016-08-10 12:23:04 -0500
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)

Next, to test puma-dev:
I turned off the server, and made these changes to development.rb:

config.action_cable.url = "ws://actioncable-examples.dev/cable"
config.action_cable.allowed_request_origins = ["http://actioncable-examples.dev"]

Then I setup puma-dev to load the app at actioncable-examples.dev and started the server with puma-dev.

Visiting http://actioncable-examples.dev I see the same errors as described above:

Firefox can’t establish a connection to the server at ws://actioncable-examples.dev/cable.

Then in the server log:

Started GET "/cable" for 127.0.0.1 at 2016-08-10 12:21:42 -0500
Started GET "/cable/"[non-WebSocket] for 127.0.0.1 at 2016-08-10 12:21:42 -0500
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: , HTTP_UPGRADE: )
Finished "/cable/"[non-WebSocket] for 127.0.0.1 at 2016-08-10 12:21:42 -0500

EDIT: I also noticed the puma version was "2.15.3". I ran bundle update puma to get it to "3.6.0", but it didn't seem to change anything.

@evanphx
Copy link
Member

evanphx commented Aug 10, 2016

The puma fork is what you want to test. It changes the config to run all in
one server with /cable mounted. Please try it as is under puma-dev and let
me know if it works for you (it does for me)
On Wed, Aug 10, 2016 at 11:24 AM Glenn Sidney notifications@github.com
wrote:

OK, I checked out actioncable-examples. It runs ActionCable on a separate
server instance, which is not the default setup, and not the situation that
I've described here. To get it looking right, I changed development.rb
with:

  • config.action_cable.url = 'ws://localhost:28080'
  • config.action_cable.url = "ws://localhost:3000/cable"
  • config.action_cable.allowed_request_origins = ["http://localhost:3000"]

and routes.rb with:

  • mount ActionCable.server => '/cable'

With this change, rails s fires up a server accessible at "
http://localhost:3000" and I can go there and test that ActionCable works
just fine. The server log shows:

Started GET "/cable" for ::1 at 2016-08-10 12:23:04 -0500
Started GET "/cable/" [WebSocket] for ::1 at 2016-08-10 12:23:04 -0500
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)

Next, to test puma-dev:
I turned off the server, and made these changes to development.rb:

config.action_cable.url = "ws://actioncable-examples.dev/cable"
config.action_cable.allowed_request_origins = ["http://actioncable-examples.dev"]

Then I setup puma-dev to load the app at actioncable-examples.dev and
started the server with puma-dev.

Visiting http://actioncable-examples.dev I see the same errors as
described above:

Firefox can’t establish a connection to the server at ws://actioncable-examples.dev/cable.

Then in the server log:

Started GET "/cable" for 127.0.0.1 at 2016-08-10 12:21:42 -0500
Started GET "/cable/"[non-WebSocket] for 127.0.0.1 at 2016-08-10 12:21:42 -0500
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: , HTTP_UPGRADE: )
Finished "/cable/"[non-WebSocket] for 127.0.0.1 at 2016-08-10 12:21:42 -0500


You are receiving this because you commented.

Reply to this email directly, view it on GitHub
#48 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAAAB8qRRasjBWGKbF67OWxtKYPWaSAjks5qegkwgaJpZM4JfYnl
.

@glennfu
Copy link
Author

glennfu commented Aug 11, 2016

Can you clarify what that means? If I google Puma and "fork" or "puma fork" I can't find any clarity on how to use this or what that means.

@evanphx
Copy link
Member

evanphx commented Aug 11, 2016

I mean https://github.com/puma/actioncable-examples. It has commits to work inside puma-dev as a test. I'm in the midst of writing a new actioncable test app as well.

@glennfu
Copy link
Author

glennfu commented Aug 12, 2016

OK I see what you mean. I just switched over to using that repo, and added config.action_cable.url = "ws://actioncable-examples.dev/cable" to it, and I see exactly the same errors.

@evanphx
Copy link
Member

evanphx commented Aug 12, 2016

Ok, but why is setting that required? It works correctly without it.
On Fri, Aug 12, 2016 at 2:59 PM Glenn Sidney notifications@github.com
wrote:

OK I see what you mean. I just switched over to using that repo, and added config.action_cable.url
= "ws://actioncable-examples.dev/cable" to it, and I see exactly the same
errors.


You are receiving this because you commented.

Reply to this email directly, view it on GitHub
#48 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAAAB6ITf36g7WRjXTucYNxRqiszbNOkks5qfN6sgaJpZM4JfYnl
.

@glennfu
Copy link
Author

glennfu commented Aug 12, 2016

If I checkout that project and don't make any changes at all, I get the same error. I can't find any configuration that uses a .dev url to host the app through puma-dev that makes ActionCable connect.

@navied
Copy link

navied commented Jan 23, 2017

Hate to resurface this, but I just realized that this issue actually still shows up for me but only on Firefox. It works completely fine on Chrome though.

I tested it out with your it with your example project as well.

@joallard
Copy link

I'm getting the exact same error as @glennfu above on latest Firefox.

I have a Rails running on a port, if I access it as localhost it works, else it fails upgrading the socket.

# *.dev
Started GET "/cable" for 127.0.0.1 at 2017-02-21 17:42:55 -0500
Started GET "/cable/"[non-WebSocket] for 127.0.0.1 at 2017-02-21 17:42:55 -0500
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: , HTTP_UPGRADE: )
Finished "/cable/"[non-WebSocket] for 127.0.0.1 at 2017-02-21 17:42:55 -0500

# localhost:3000
Started GET "/cable" for ::1 at 2017-02-21 17:43:00 -0500
Started GET "/cable/" [WebSocket] for ::1 at 2017-02-21 17:43:00 -0500
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)

@joallard
Copy link

Is it me or does puma-dev mangle Connection and Upgrade headers?

@mxmzb
Copy link

mxmzb commented Jun 17, 2017

This issue is killing me for two days, any ideas? It doesn't work with any browser for me and I also tried everything in this thread by now. Here is a SO questioned I also opened about this: https://stackoverflow.com/questions/44602711/actioncable-never-establishes-connection-to-channel

@abury
Copy link

abury commented Aug 22, 2017

I haven' been able to get it to work at all with puma-dev. I kept getting handshake issues and the server wasn't even logging any attempts at a connection 😞

Doing a manual rails server startup and creating a connection to ws://localhost:3000 worked without a hitch.

@danielwestendorf
Copy link

I can confirm that I'm unable to get websockets to work properly when accessing an app served by puma-dev with Firefox, but it works with Chrome.

When I access the same app with a simple rails s with Firefox, the websockets connect just fine.

Started GET "/cable" for 127.0.0.1 at 2017-10-30 11:48:40 -0600
Started GET "/cable/"[non-WebSocket] for 127.0.0.1 at 2017-10-30 11:48:40 -0600
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: , HTTP_UPGRADE: )

How can I help troubleshoot this issue?

@joallard
Copy link

Trying to figure out what to change in the codebase is probably a good start! ;)

@danielwestendorf
Copy link

@joallard Yes, of course. If I knew golang and this project better, there would probably already be a PR. Thanks for the suggestion though ;)

@krasnoukhov
Copy link
Contributor

Seems like I was able to find the root cause of this issue. Basically Firefox sends Connection: keep-alive, Upgrade, which isn't parsed correctly by puma-dev since it assumes it should be just Upgrade for a websocket request. #143 fixes that by checking that the value ends with Upgrade. Feel free to give it a spin and let me know if it helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants