leahpleurodon
leahpleurodon

Reputation: 81

Action Cable only sending welcome and ping message to client

I am able to get my Action Cable working and the subscription to work without any dramas...

For now, I have a simple HTML page with some JS on it to connect and log the WebSocket messages.

This is the script I'm using (for now to test) on my HTML page

    var socket = new WebSocket('ws://localhost:3000/cable');
    socket.onopen = function(e) {
        console.log("Connection established")
    }
    socket.onclose = function(e) {
        console.log("Error occurred.");
        clients.Remove(socket)  
    }
    socket.onmessage = function(e){
        var server_message = e;
        console.log(server_message);
    }

    JSON.stringify(
        {
            "command": "message",
            "identifier": JSON.stringify({"channel": "tv_channel_1e80f418-08b0-478a-a77c-67a052934433" }),
            "data": {
                "action": "start_broadcasting"
            }
         }  
    ) 

My rails output has confirmed that the subscription is there and my page is console logging the ping messages and the welcome message from the websocket.

If I try to broadcast anything even as a test (through the console or otherwise)

ActionCable.server.broadcast "tv_channel_1e80f418-08b0-478a-a77c-67a052934433", {message: "oh hai"}

it does not get picked up by the client...

I have Redis all installed via brew and the following in my cable.yml file

development: &development
  adapter: redis
  url: redis://localhost:6379

test:
  adapter: redis
  url: redis://localhost:6379

production:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
  channel_prefix: can-i-haz-dashboard-server_production

I'm able to confirm through redis-cli that the messages are going through to the redis db.

The only answers I can find involve installing redis.

My connection.rb file (channel coded for testing)

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

    def connect
      self.current_channel = ::Channel.find("1e80f418-08b0-478a-a77c-67a052934433")
    end

    private
      def find_channel

      end
  end
end

my TvChannel.rb

class TvChannel < ApplicationCable::Channel
  def subscribed
    # stream_from "some_channel"
    stream_from "tv_channel_#{current_channel.id}"
    ActionCable.server.broadcast "tv_channel_#{current_channel.id}", 'Test message'
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end

  def start_broadcasting
    "hello"
  end

end

I hope what I'm doing wrong is simple. Any help would be greatly appreciated.


Edit: Sorry, not sure if I was clear enough. My Rails app is API only for the websocket, there is no view I'm serving it to. The HTML page is standalone and not in the Rails environment.

Upvotes: 0

Views: 2624

Answers (1)

leahpleurodon
leahpleurodon

Reputation: 81

Sooooo I hope if nothing else, this helps someone else.

I needed to actually send the subscription string.

    socket.onopen = function(e) {
        console.log("Connection established")
        socket.send(subscribeString)
    }

    subscribeString = JSON.stringify(
        {
            "command": "subscribe",
            "identifier": JSON.stringify(
                {"channel": "channels:1e80f418-08b0-478a-a77c-67a052934433" }
                 )
        }  
    )

Upvotes: 4

Related Questions