Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

actioncable can't connect to websocket

I can't connect to the actioncable websocket.

I added this line to my routes.rb

match "/websocket", :to => ActionCable.server, via: [:get, :post]

and I added this files to my app path: cable/config.ru

# cable/config.ru
require ::File.expand_path('../../config/environment',  __FILE__)
Rails.application.eager_load!

require 'action_cable/process/logging'

run ActionCable.server

channels/application_cable/channel.rb

module ApplicationCable
  class Channel < ActionCable::Channel::Base
  end
end

channels/application_cable/connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    protected
      def find_verified_user
        if current_user
          current_user
        else
          reject_unauthorized_connection
        end
      end
  end
end

and my javascriptfiles under assets/javascript/channels/index.coffee

#= require cable
#= require_self
#= require_tree .

@App = {}
# App.cable = Cable.createConsumer 'ws://localhost:28080'
# App.cable = Cable.createConsumer 'ws://localhost/websocket'

then I started my rails app with:

TRUSTED_IP=192.168.0.0/16 rails s -b 192.168.56.101 Puma

and my ActionCalbe-Server with:

bundle exec puma -p 28080 cable/config.ru

When I opened my Application in the browser I get this errors:

Firefox kann keine Verbindung zu dem Server unter ws://localhost:28080/ aufbauen.
Firefox kann keine Verbindung zu dem Server unter ws://localhost/websocket aufbauen.

How I can solve my problem? I didn't find the mistake :/

like image 806
Evolutio Avatar asked Nov 09 '22 02:11

Evolutio


1 Answers

Your find_verified_user method makes no sense, it will always fail. Change it to find a user based on the presence of some (signed) cookie.

def find_verified_user
  if user = User.find_by(id: cookies.signed[:user_id])
    user
  else
    reject_unauthorized_connection
  end
end
like image 103
aromero Avatar answered Nov 14 '22 21:11

aromero