Group Chat with ActionCable: Part 4 Discussion
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
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.
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.
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.
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.
Okay I mostly have my version of this tutorial working. however, my chat message shows the users avatar next to it. When redis does the job and adds the message to the div it prepends http://example.org/ to the image along with some other weird stuff creating a broken link. If I refresh the page the image is displayed correctly as an active_storage file from my app using my app's URL. How do I get methods that are called by redis jobs to know what host to prepend to url's that it creates? thanks
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
I tried this but the request.base_url is reporting as Http://example.org at that point. Hmm
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.
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.