I need to make realtime updates for all subscribers. After creating new
record i need to publish it for all subscribers.
I found a solution using postgre listen/notify
In controller:
def index
response.headers['Content-Type'] = 'text/event-stream'
sse = SSE.new(response.stream)
begin
Comment.on_change do |data|
sse.write(data)
end
rescue IOError
# Client Disconnected
ensure
sse.close
end
render nothing: true
end
and model
[...]
after_create :notify_comment_added
[...]
private
def notify_comment_added
Comment.connection.execute "NOTIFY comments, 'data'"
end
class << self
def on_change
Comment.connection.execute "LISTEN comments"
loop do
Comment.connection.raw_connection.wait_for_notify do |event, pid, comment|
yield comment
end
end
ensure
Comment.connection.execute "UNLISTEN comments"
end
end
Problems are
1) Every listeners occupies a connection to db, and after some clients its
reach maximum and i got ActiveRecord timeout
2) it works only with config.cache_classes = true and config.eager_load =
true otherwise servers froze on 1-2nd execution of controller. And its a
big pain to restart server after every small change.
Does rails have any another solutions to perform this task?
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/0ec03c37-a117-4845-ad10-d02434669043%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.