Ask A Question

Notifications

You’re not receiving notifications from this thread.

Group Chat with ActionCable: Part 4 Discussion

You sir, are awesome! This is exactly what I've been looking for.

Reply

Thanks for the tutorial but I'm struggling with understanding some of the concepts. This tutorial (and everyone I can find online) seems to assume actioncable is to be loaded on every page on the site. Is there any direction on say you only want this stuff happening on the ChatPage. not the About US page or the Main page or before a user has signed in. I cannot find any guidance on turning this off/on only if you are on the correct page that will be using it. Thanks

Reply

ActionCable should be on every page and always connected. You don't want to disconnect and reconnect constantly.

You simply want to ignore updates if they aren't relevant to the page you're looking at.

Reply

Can you explain why I want actioncable running on the about page, the contact us page, or any page before there is even a user signed in? Seems a lot of unnecessary traffic processing when it will not be used. Say my site has 50 pages only 1 of which even uses actioncable. Thanks.

Reply

Websockets are just designed to be a persistent connection. As you're browsing the site the connection can stay idle until you need it. GitHub, for example, will leave the websocket connection open as you browse but only subscribe to receive events when you view or leave a Pull Request or Issue.

Reply

And you can definitely configure it to only create the websocket connection on the page you want like what you're describing. Just gotta write your JS to detect the page and connect when it's the correct one.

Reply
Reply

This is the applicationhelper that is called

def profile_picture_with_class user, css_class, shape, width = 100
    if (shape == :square)
        placeholder_pic = "blank_profile_square.jpg"
    else
        placeholder_pic = "blank_profile_round.png"
    end
    image_path = user.profile_image.present? ? user.profile_image : placeholder_pic
    image_tag(image_path, width: width, class: css_class)
end 
Reply
Reply

I tried this but the request.base_url is reporting as Http://example.org at that point. Hmm

Reply
renderer = ChatMessagesController.renderer.new(
  http_host: ENV['RAILS_APPLICATION_URL'].presence || 'http://localhost:3000' ,
  https:      Rails.env.production?
)

ActionCable.server.broadcast "stream:#{chat_message.stream.id}", {
    chat_message: renderer.render(chat_message)         
}

Had to set the host and ssl to get it to work, the defaults are going back to example.com . SSL was important to set right without it it did not work.

Reply

Hi Chris,

Did you already get that kind of error message :

ActionView::Template::Error (Devise could not find the `Warden::Proxy` instance on your request environment.
Make sure that your application is loading Devise and Warden as expected and that the `Warden::Manager` middleware is present in your middleware stack.

Tryed many things but still under trouble. I saw same cases talking about include Devise::Test::ControllerHelpers inside my rails_helpers.rb but nothing change.

Many thanks.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.