Is it possible to use Turbo Streams with MongoDB in a Rails 7 application?

89 Views Asked by At

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

There are 2 best solutions below

0
Les Nightingill On

(I haven't verified or tried this!) It looks as if you can include the Turbo::Broadcastable concern, and it will provide the broadcasts_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)."

0
Maxence On

I broadcast from a controller rather than a model with : Turbo::StreamsChannel.broadcast_replace_to You may check if this could work