I am trying to create a reactive chat application with Rails 7 using MongoDB.
I have three models that I’m working with: 1. Topic 2. TopicMessage 3. User
For the MongoDB setup, it is required to use Mongoid and the models cannot inherit from ‘ApplicationRecord’
An error occurs when trying to use broadcasts_to due to not inheriting from ApplicatioRecord
Is there a way around this?
I get the following error from attempting this code
class Topic
include Mongoid::Document
include Mongoid::Timestamps
field :parent_thread, type: Integer
field :name, type: String
field :description, type: String
field :url_image, type: String
field :community_id, type: String
broadcasts_to ->(topic){:topics_list}
end
undefined method `broadcasts_to’ for Topic:Class
2
Answers
(I haven’t verified or tried this!) It looks as if you can include the
Turbo::Broadcastable
concern, and it will provide thebroadcasts_to
class method.See https://github.com/hotwired/turbo-rails/blob/main/app/models/concerns/turbo/broadcastable.rb
"Turbo streams can be broadcast directly from models that include this module (this is automatically done for Active Records)."
I broadcast from a controller rather than a model with :
Turbo::StreamsChannel.broadcast_replace_to
You may check if this could work