Ronan Louarn
Ronan Louarn

Reputation: 472

Nothing happens with actionCable rails 5.0.1

It's not my first websocket installation in rails but when i type that in my rails console, nothing happens. That should print 'ok' in Chrome console no? Connected function works but not received, what is wrong with this code? I replaced async by redis, nothing change.

NotificationsChannel.broadcast_to(User.find_by(email: "[email protected]"), title: "ok")

logs in rails c

[ActionCable] Broadcasting to notifications:Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw: {:title=>"ok"}

=> nil

my terminal rails s

[ActionCable] [8857] Finished "/cable/" [WebSocket] for ::1 at 2017-09-

13 11:38:00 +0200
[ActionCable] [8857] NotificationsChannel stopped streaming from notifications:Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw
Started GET "/cable" for ::1 at 2017-09-13 11:38:00 +0200
Started GET "/cable/" [WebSocket] for ::1 at 2017-09-13 11:38:00 +0200
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 8857], ["LIMIT", 1]]
[ActionCable] [8857] Registered connection (Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw)
[ActionCable] [8857] NotificationsChannel is transmitting the subscription confirmation
[ActionCable] [8857] NotificationsChannel is streaming from notifications:Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw

There is my webSocket installation:

channel/application_cable/connection.rb

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

    def connect
      self.current_user = find_verified_user
      logger.add_tags 'ActionCable', current_user.id
    end

    protected

    def find_verified_user 
      if verified_user = env['warden'].user
        verified_user
      else
       reject_unauthorized_connection
      end
    end
  end
end

app/channels/notifications_channel.rb

class NotificationsChannel < ApplicationCable::Channel


  def subscribed
    stream_for current_user
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

app/assets/javascript/channels/notifications.coffee

App.cable.subscriptions.create channel: "NotificationsChannel",
  connected: ->
    console.log "connected"

  received: (data) ->
    console.log "ok"

I don't get anything in my frames when i broadcast.

Upvotes: 2

Views: 389

Answers (1)

Ronan Louarn
Ronan Louarn

Reputation: 472

I found the problem ==> Rails version.

Just update rails 5.0.1 to 5.0.2 if you are in this case.

And replace async in cable.yml with redis.

Upvotes: 1

Related Questions