File app / channels / conversation_channel.rb:

class ConversationChannel < ApplicationCable::Channel def subscribed stream_from "conversations-#{current_profile.id}" end def unsubscribed stop_all_streams end def speak(data) message_params = data['message'].each_with_object({}) do |el, hash| hash[el.values.first] = el.values.last end Message.create(message_params) end end 

stream_from "conversations-#{current_profile.id}" - subscribes to the conversation and to its stream

File job / message_broadcast.rb:

 class MessageBroadcastJob < ApplicationJob queue_as :default def perform(message) sender = message.profile recipient = message.conversation.opposed_profile(sender) broadcast_to_sender(sender, message) broadcast_to_recipient(recipient, message) end private def broadcast_to_sender(profile, message) ActionCable.server.broadcast( "conversations-#{profile.id}", message: render_message(message, profile), conversation_id: message.conversation_id) end def broadcast_to_recipient(profile, message) ActionCable.server.broadcast( "conversations-#{profile.id}", window: render_window(message.conversation, profile), message: render_message(message, profile), conversation_id: message.conversation_id) end def render_message(message, profile) ApplicationController.render( partial: 'messages/message', locals: { message: message, profile: profile }) end def render_window(conversation, profile) ApplicationController.render( partial: 'conversations/conversation', locals: { conversation: conversation, profile: profile }) end end 

Here is the broadcast. But you have to reload the page to display the message.

When sending a message to the user who sent the message is shown, the other does not. This can be seen in the console:

 [ActiveJob] [MessageBroadcastJob] [42a34e05-37c3-46d3-bace- eb5eac79e41b] Rendered messages/_message.html.erb (1.1ms) [ActiveJob] [MessageBroadcastJob] [42a34e05-37c3-46d3-bace-eb5eac79e41b] [ActionCable] Broadcasting to conversations-1: {:message=>"<li>\n <div class=\"row\">\n <div class=\"message-sent\">\n 201\n </div>\n </div>\n</li>\n", :conversation_id=>1} [ActiveJob] [MessageBroadcastJob] [42a34e05-37c3-46d3-bace-eb5eac79e41b] Rendered conversations/_conversation.html.erb (15.5ms) [ActiveJob] [MessageBroadcastJob] [42a34e05-37c3-46d3-bace-eb5eac79e41b] Error performing MessageBroadcastJob (Job ID: 42a34e05-37c3-46d3-bace-eb5eac79e41b) from Async(default) in 57.15ms: ActionView::Template::Error (Missing partial application/_conversation_content with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :slim, :jbuilder]}. Searched in: * "/home/steepbeaver/Prespy/app/views" * "/var/lib/gems/2.3.0/gems/devise-4.3.0/app/views"): ConversationChannel transmitting {"message"=>"<li>\n <div class=\"row\">\n <div class=\"message-sent\">\n 201\n </div>\n </div>\n</li>\n", "conversation_id"=>1} (via streamed from conversations-1) 

Thanks in advance for your comments and replies. At any time ready to supplement the information!

  • So correct the mistake that was quoted for a start. - D-side

1 answer 1

The answer was here:

[ActiveJob] [MessageBroadcastJob] [42a34e05-37c3-46d3-bace-eb5eac79e41b] Error performing MessageBroadcastJob (Job ID: 42a34e05-37c3-46d3-bace-eb5eac79e41b) from Async(default) in 57.15ms: ActionView::Template::Error (Missing partial application/_conversation_content with {:locale=>[:en], :formats=>[:html, :text, :js, ........]}

In the app/views folder, it was necessary to create the application folder with the _conversation_content file

  • Here you go. You can, when you want :) - D-side