Quantcast
Channel: Active questions tagged redis+ruby-on-rails - Stack Overflow
Viewing all articles
Browse latest Browse all 873

ActionCable is unable to receive data

$
0
0

I am creating a messaging app in Rails 6. Where I have a controller called chatroom, when a registered user enters message, it calls messages_controller.

The messages_controller.rb looks like this:

class MessagesController < ApplicationController    before_action :require_user    def create        message = current_user.messages.build(message_params)        if message.save            # redirect_to root_path            ActionCable.server.broadcast 'chatroom_channel',                { title: message.body, body: message.body }        end    end    private    def message_params        params.require(:message).permit(:body)    endend

The chatroom_channel.js under javascript/channels:

import consumer from "./consumer"consumer.subscriptions.create("ChatroomChannel", {    connected() {        // Called when the subscription is ready for use on the server    },    disconnected() {        // Called when the subscription has been terminated by the server    },    received(data) {        // Called when there's incoming data on the websocket for this channel        window.alert('hi')        window.console.log('hi')    }});

But for some reason, I can't see any alert or logging to the browser.

The standard output from the server after a message is entered:

Started POST "/message" for 127.0.0.1 at 2020-08-27 03:02:08 +0530Processing by MessagesController#create as HTML  Parameters: {"authenticity_token"=>"dhkn32ve1N4eIK4Vgg00KcfP/kuEhfhPHGvTLXKlD2c1rtB/9DQTNudSME7sKL4cxe3nS2lQexXhtdLES9mHjg==", "message"=>{"body"=>"Hello, this is a message!!"}, "commit"=>""}  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]↳ app/controllers/application_controller.rb:5:in `current_user'   (0.1ms)  begin transaction↳ app/controllers/messages_controller.rb:7:in `create'  Message Create (1.2ms)  INSERT INTO "messages" ("body", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["body", "Hello, this is a message!!"], ["user_id", 1], ["created_at", "2020-08-26 21:32:08.730711"], ["updated_at", "2020-08-26 21:32:08.730711"]]↳ app/controllers/messages_controller.rb:7:in `create'   (5.8ms)  commit transaction↳ app/controllers/messages_controller.rb:7:in `create'[ActionCable] Broadcasting to chatroom_channel: {:title=>"Hello, this is a message!!", :body=>"Hello, this is a message!!"}No template found for MessagesController#create, rendering head :no_contentCompleted 204 No Content in 28ms (ActiveRecord: 7.4ms | Allocations: 4404)Started GET "/cable" for 127.0.0.1 at 2020-08-27 03:02:15 +0530Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2020-08-27 03:02:15 +0530Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)ChatroomChannel is transmitting the subscription confirmation

The config/cable.yml file has this content:

development:  adapter: redis  url: redis://localhost:6379/1test:  adapter: testproduction:  adapter: redis  url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>  channel_prefix: MessageMe_production

I have redis gem in the Gemfile, and also redis server running on 6379. The rails server is running on port 8080.

routes.rb:

Rails.application.routes.draw do  mount ActionCable.server, at: '/cable'  post 'message', to: 'messages#create'  delete 'logout', to: 'sessions#destroy'  post 'login', to: 'sessions#create'  get 'login', to: 'sessions#new'  get 'chatroom/index'  get 'chatroom', to: 'chatroom#index'  get 'chatroom/help'  root to: 'chatroom#index'end

If I insert a console.log('Hello!') inside the connected() function I can see that in the browser.

Yes, the data is getting saved, and if I put some code in if message.save that is executed. Yet, ActionCable's receive() not working.

Am I missing something?


Viewing all articles
Browse latest Browse all 873

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>