0

I've looked at multiple tutorials and cannot seem to receive notifications from action cable when I create a new Message record. All I am trying to do is print out the response in the console just so I know it works. I've installed redis but still no luck. Here are my files. Any help would be appreciated.

cable.yml

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

production: *redis
development: *redis
test: *redis

application.js

//= require jquery
//= require bootstrap-sprockets
//= require rails-ujs
//= require turbolinks
//= require_tree ./channels

cable.js

// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
//
//= require action_cable
//= require_self
//= require_tree ./channels

(function() {
  this.App || (this.App = {});

  App.cable = ActionCable.createConsumer();

}).call(this);

messages.js

//= require cable
//= require_self

(function() {
    // Subscrive to the class name of the channel
    App.messages = App.cable.subscriptions.create('MessagesChannel', {
    /**
     * Whenever this channel pushes content, it is received here
     */
    received: function(message) {
        console.log(message);

    }
  });
}).call(this);

channels/messages.rb

class MessagesChannel < ApplicationCable::Channel
  def subscribed
    stream_from 'messages'
  end
end

models/message.rb

class Message < ApplicationRecord

    after_save :broadcast_save

    def broadcast_save
       ActionCable.server.broadcast 'messages', html: render_task
    end

    private
    def render_task
       ApplicationController.render(partial: 'messages/message', locals: { message: self })
    end
  end

application.html.erb head element

 <head>
    <title>Test</title>
    <%= action_cable_meta_tag %>
    <%= csrf_meta_tags %>
    <%= javascript_include_tag "//static.twilio.com/libs/twiliojs/1.2/twilio.min.js" %>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

  </head>

For testing purposes, I just create a new message when the controller action is called.

class PhoneController < ApplicationController

    def index
        @tickets = []
        Message.create(body: "Test")
    end
end

console output for rails

Started GET "/phone" for 75.65.92.24 at 2017-11-08 00:54:15 +0000
Cannot render console from 75.65.92.24! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PhoneController#index as HTML
   (0.1ms)  BEGIN
  SQL (0.7ms)  INSERT INTO "messages" ("body", "created_at", "updated_at") 
VALUES ($1, $2, $3) RETURNING "id"  [["body", "Test"], ["created_at", "2017-
11-08 00:54:15.192006"], ["updated_at", "2017-11-08 00:54:15.192006"]]
  Rendered messages/_message.html.erb (0.5ms)
[ActionCable] Broadcasting to messages: {:html=>"asdf"}
   (5.4ms)  COMMIT
  Rendering phone/index.html.erb within layouts/application
  Rendered phone/index.html.erb within layouts/application (0.8ms)
Completed 200 OK in 77ms (Views: 60.7ms | ActiveRecord: 6.3ms)

redis console output (last line)

[37939] 08 Nov 00:49:19.158 * The server is now ready to accept connections on port 6379
Cannon Moyer
  • 3,014
  • 3
  • 31
  • 75

1 Answers1

0

seems like your issue lies here:

Cannot render console from 75.65.92.24! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

You might find this question helpful:

How to disable "Cannot Render Console from..." on Rails

Ti Zhang
  • 17
  • 7