I use Action Cable for chat with various channels. Everything is configured using Redis and Postgresql (to save messages) on Heroku, and 95% of the time works fine in development and production.
Sometimes, although sent messages are not displayed. The message is sent, it is stored in the database, but then it never appears on the front side if I do not update. Or the message appears in another channel for which it is not directed. Again, everything is stored correctly in the Postgres database. He just becomes awkward at the front end. Somewhere, the ActionCable seems to be confused.
This problem rarely occurs to me, which is very difficult to replicate for proper debugging. But I have a bunch of users who regularly report a problem, and I'm struggling to figure out how to track it.
Here is my code:
JavaScripts / channels / channels.js
class PodsChannel < ApplicationCable::Channel def subscribed stream_from "pods_channel_
Channels / pods_channel.rb
$(document).on("ready",function(){ var pod_slug = $("#pod_slug_value").val();
models / pod_message.rb
class PodMessage < ApplicationRecord after_create_commit { MessageBroadcastJob.perform_now self } end
Work / message_broadcast_job.rb
class MessageBroadcastJob < ApplicationJob queue_as :default def perform(pod_message) stream_id = "pods_channel_#{pod_message.pod_slug}" ActionCable.server.broadcast stream_id, message: render_message(pod_message), msg_id: pod_message.id, pod_slug: pod_message.pod_slug end private def render_message(pod_message) renderer = ApplicationController.renderer.new renderer.render(partial: 'pod_messages/pod_message', locals: { pod_message: pod_message }) end end
source share