ruby-on-railswebsocketruby-on-rails-4.1pumawebsocket-rails

Websockets with Rails(Puma) - Error during WebSocket handshake: Unexpected response code: 200


I am trying to use websocket in my Rails4.1 application

Here are some relevant code snippets:

Gemfile:

gem 'websocket-rails'
gem 'puma'

development.rb

 config.middleware.delete Rack::Lock

I am starting the server locally as:

bundle exec puma -p 3000

In the chrome console I see a connection error:

new WebSocketRails('localhost:3000/post/hello', true);

WebSocket connection to 'ws://localhost:3000/post/hello' failed: Error during WebSocket handshake: Unexpected response code: 200

Can anyone help with what I need to do to use web sockets locally in Rails?


Update1

I tried adding following as per Websockets not working in my Rails app when I run on Unicorn server, but works on a Thin server but it did not help

initializers/eventmachine.rb

Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive

Solution

  • Try this in your console:

    window.dispatcher = new WebSocketRails window.document.location.host + '/websocket'
    

    You configure the rest of what you want to do in the config/events.rb file and whatever controllers you use to handle the events