All threads / How to subscribe to multiple chat rooms in a chat using Action Cable

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to subscribe to multiple chat rooms in a chat using Action Cable

Drazen Mokic asked in General

All examples i can find are subscribing to one single room by pasing a room as a param when creating the channel but what if i have Room A, B and C an i (as a user) am in all of them.

This is an example with one room http://edgeguides.rubyonrails.org/action_cable_overview.html#client-server-interactions-subscriptions

I'm doing a series of videos on this as we speak. :)

Basically you'll want to keep track of them server side and then on connect, you'll loop through the connected rooms and stream from each.

Awesome :)

How do you subscribe on the client to multiple rooms ?

Bump :)

Do you loop through all the rooms also on the client side and call App.cable.subscriptions.create for each channel or is there a more straight forward way like subscribing to multiple channels at once?

Hey Drazen,

Handful of different ways to do this. If you know which channels the user is joined to server-side, you can just loop through them and then stream_from for each channel. That will setup several redis pubsub connections so you will receive all the messages across those channels.

Join the discussion

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

Join 37,629+ 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.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.