[ActionCable] Filter data based on params

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[ActionCable] Filter data based on params

buhrmi
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/60176de6-b536-4d21-a21e-4c00727542df%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [ActionCable] Filter data based on params

buhrmi
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 [hidden email].
To post to this group, send email to [hidden email].
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.