Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live / Streaming API? #6

Closed
activestylus opened this issue Sep 25, 2014 · 4 comments
Closed

Live / Streaming API? #6

activestylus opened this issue Sep 25, 2014 · 4 comments

Comments

@activestylus
Copy link

Not an issue as much as an inquiry. I am THIS close to selling my team on Roda, but we do need a bit of async action, mostly in the form of SSE, though there could be room for a full on pub/sub solution down the line.

Curious if you have ever dealt with these specs or how plausible it even is within the scope of a plugin. My own research shows people reaching a dead end in terms of mixing synchronous with asynchronous code, like the ill-fated Rack Stream library.

I am personally considering Rack AMQP to fill this void, but the jury is still out until I can really kick the tires on this badboy.

Thoughts?

@jeremyevans
Copy link
Owner

The streaming plugin handles streaming, and I use it for SSE in one of my apps. I'm using LISTEN in PostgreSQL for updates and a Queue for synchronization. See http://roda.jeremyevans.net/rdoc/classes/Roda/RodaPlugins/Streaming.html for details on the streaming support. Here's a basic idea of what I'm doing:

# outside your Roda.route block
Thread.new do
  DB.listen('channel_name', :loop=>proc{QUEUES.each{|q| q.push('')}}, :timeout=>60) do |_, _, msg|
    QUEUES.each{|q| q.push(msg)}
  end
end

# inside your Roda.route block
r.get "streaming" do
  response['Content-Type'] = 'text/event-stream;charset=UTF-8'
  response['X-Accel-Buffering'] = 'no' # for nginx
  q = Queue.new
  QUEUES.each{|q| q.push('')}
  QUEUES << q
  stream(:loop=>true, :callback=>proc{QUEUES.delete(q)}) do |out|
    out << "data: #{q.pop}\n\n"
  end
end

Note that as this isn't a bug in Roda, per the contribution guidelines, you should probably be asking about it on the ruby-roda Google Group or the IRC channel.

@activestylus
Copy link
Author

Thanks for the quick response and duly noted - I wiIl post further queries of this nature in the group.

I continue to be impressed by how many batteries come included in this tiny framework. Bravo!

@tobymao
Copy link

tobymao commented Mar 22, 2020

This guide helped me, but I couldn't get messages streamed in until i set

response['Transfer-Encoding'] = 'identity'

@makevoid
Copy link

makevoid commented Sep 3, 2022

thanks @tobymao for pointing this out, I had some code that wasn't working anymore on latest Chrome and adding response['Transfer-Encoding'] = 'identity' made it work again! many thanks!

I think someone should probably update Roda's streaming plugin documentation with your note https://github.com/jeremyevans/roda/blob/master/lib/roda/plugins/streaming.rb#L6

The alternative I guess is to provide an example implementation of chunked (default) encoding where apparently you need to pass the size of the chunk in hex and /r/n as separators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants