Rendering a model into different views through Hotwire

Hello,

this is a general concern I have with the HTML-over-wire approach.

Let’s assume I have an Event model which belongs_to a Calendar. The user can show the calendar in different views, eg. yearly, monthly, weekly, etc.

Now a new Event is being created, and I want to broadcast it to the users. But the HTML for an event is different depending on which calendar view the user currently is looking at.

How would I implement something like this with the HTML-over-wire approach?

I think that one solution could be to use CSS to display the same HTML for an event differently in different calendar views.

You can try having separate streams per view, or sending multiple turbo action tags over 1 stream, whichever you’re more comfortable with

2 Likes

I personally don’t prefer sending view specific data in web-socket events. I treat web sockets as a notification system and then the clients can decide if they want to fetch the data after the event by making a regular call or not.

For example: A chat has received a reply. The web-socket will notify the clients that a new message has been received and then the clients can trigger fetch or Ajax request to get the new message.

If you follow this approach, then each client can send its current view to the backend during the fetch request and backend can return an appropriate HTML fragment for it

1 Like