Display chat messages in modal

kjdelys kjdelys posted on 2025-04-08 22:12:06 UTC

Hello guys,
i'm using rails 7.2 with esbuild, preline.js and tailwind. I would like to get some feedback about the best strategy do implement chat messages inside a popup or modal. Live refresh (appending new messages) in modal does not work properly with 
<%= turbo_stream_from @conversation %>

You have conversations and messages belongs to conversations.
I implemented a stimulus controller that takes controller output, opens the offcanvas and replace content with controller output.


import {Controller} from "@hotwired/stimulus";
import {HSOverlay} from "preline/dist/preline";

export default class extends Controller {
    static targets = ["content"];

    async fetchData(event) {
        event.preventDefault(); // Prevent default behavior

        // Get the URL to fetch data from
        const url = event.currentTarget.href || event.currentTarget.dataset.url;

        // Get the offcanvas ID from the button's data attribute
        const offcanvasId = event.currentTarget.dataset.offcanvas;

        try {
            const response = await fetch(url);
            const data = await response.text(); // Assuming the response is HTML

            // Find the offcanvas content container
            const contentTarget = document.querySelector(`#${offcanvasId} [data-overlay-target="content"]`);

            // Update the offcanvas content
            if (contentTarget) {
                contentTarget.innerHTML = data;
            }

            // Reinitialize Preline components within the new content
            window.HSStaticMethods.autoInit();

            // Initialize and open the offcanvas
            const offcanvasElement = document.getElementById(offcanvasId);
            if (offcanvasElement) {
                const offcanvasInstance = new HSOverlay(offcanvasElement);
                offcanvasInstance.open();
            }
        } catch (error) {
            console.error("Error fetching data:", error);
        }
    }
}

Any idea?

kjdelys PRO said about 1 year ago :
i use the stimulus controller to display conversation chat messages

OZVID Technologies said about 1 year ago :
To enable live chat updates in a modal, ensure <%= turbo_stream_from @conversation %> is included after the modal content loads. Reconnect the stream using your Stimulus controller when the modal opens. This ensures real-time updates work correctly inside dynamic elements.

David Kimura PRO said about 1 year ago :
Honestly, this looks like it was generated by a LLM. There are several decisions here like 
data-overlay-target="content"
Which could be 
this.contentTarget

What does the view look like?

kjdelys PRO said about 1 year ago :
Hey David,
i don't understand the link between the overlay target and the fact that i would like to enable turbo stream in a modal page. Please can you explain?


David Kimura PRO said about 1 year ago :
The note about the overlay target was separate from the streaming.

I would put the turbo_stream_from in the code where it made sense. If the modal is being dynamically rendered (so once you close it or another modal pops up and replaces this chat one) then it would make sense to have the turbo_stream_from inside of the modal so it will be disposed when the modal is replaced or destroyed.

If you want to receive the streams even when the modal is closed or not preset (updating some other parts of the page like an unread message count) then it should exist outside of the modal code.

Hope this makes sense.

Login to Comment
Back to forums