All threads / Send ActionCable broadcast from javascript
Ask A Question

Notifications

You’re not receiving notifications from this thread.

Send ActionCable broadcast from javascript

Chris Valentine asked in Javascript

In the tutorials here, I've seen an ActionCable broadcast be initiated by a chat message save. The broadcast is sent via a relay to a background job and then the chatmessages on the clients update when that broadcast job is run.

In my app, i have a host that runs a chatroom. I'd like the host to click a button and have that button run a javascript command to tell ActionCable to send the "disable-chat" command to the clients that are in the host's chat. I know how the clients receive that message thru ActionCable but how can the host send that request. There is no database save going on and no form to submit.

I imagine in javascript on the hosts page there is something like . chat_consumer.send('chatroom.disable'); or something like that, without doing a form that submits the requests. How do I go about this? Thx

i realize this was just a $.ajax() call so it was taken care of. Thanks.

Join the discussion

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

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

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

    logo Created with Sketch.

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

    © 2022 GoRails, LLC. All rights reserved.