Authentication and Devise with broadcasts

This almost worked for me, but the issue I ran into if another user (or myself on another device) created a message, it’d be broadcast to “messages” stream instead of the current_user one. So I would not receive the new broadcasted message.

I ended up including the message.user_id as a data attribute in the partial. I then used CSS to hide it if it didn’t match the current user based on this How to pass `current_user.id` to a controller? - #4 by javan

Another solution I considered was storing the current user in the DOM, and then in the head tag have <meta name="current-current-id" content="<%= current_user.id %>">. I could then use a stimulus controller to retrieve current_user.id and hide certain elements(delete/edit buttons) if they didn’t match the message.user_id. The simpler CSS one solved my use case for now so I stuck with that.

2 Likes