Ruby on Rails Tuesday, October 30, 2018

Ok, just dug into the source code and think I found a solution:

stream_from 'some_channel', coder: ActiveSupport::JSON do |person|
  transmit
(thing) if (person['age'].to_i > params[:min_age])
end


On Wednesday, October 31, 2018 at 2:30:44 AM UTC+9, Stefan Buhrmester wrote:
Hello all,

I've been searching a lot but wasn't able to find a solution for this (what I thought should be a really "simple" thing).

Basically, what I want to do is to create a subscription where the messages that are received by the subscriber are filtered based on the passed params.

For example, something like this:

class PeopleChannel < ApplicationCable::Channel
 # only send people that are older than params[:min_age]
  def subscribed
   stream_for Person do |person|
      person
.age > params[:min_age]
   end
 end
end

Or, what would be a kick-ass feature but probably difficult to implement, would be to stream from an active record relation, and send only the records that are included in the relation (even if they have just been created).

class PeopleChannel < ApplicationCable::Channel
 # only send things that are older than some age
  def subscribed
    peo
ple = Person.where('age > ?', params[:min_age])
   stream_for people
 end
end

Anybody got any ideas how to do this? Filtering messages seems like such a basic thing but I can't find a way.

Stefan

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/055271fc-5d6e-47b4-ab60-8a6886868960%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment