This isn't the first time I've installed WebSockets in Rails, but when I enter this code into my Rails console, nothing happens. It should print 'ok' in the Chrome console, right? The connected function works, but the message is not received. What could be wrong with this code? I tried replacing async with redis, but it didn't make any difference.
NotificationsChannel.broadcast_to(User.find_by(email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2256475156624f434b4e0c414d4f">[email protected]</a>"), title: "ok")
Logs in rails c
[ActionCable] Broadcasting to notifications:Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw: {:title=>"ok"}
=> nil
My terminal and Rails server output
[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
Here 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'm not seeing anything when I broadcast a message.