Cera
Cera

Reputation: 1909

Is it possible to sending a streaming response from a simple Ruby / Rack app?

I'm building a Ruby web service which accepts POST or PUT requests that tell it to do some work. The work involves moving some big files around - sometimes over a network - so these requests could take a while. I'm building a separate service - in Python, or Node, or whatever - that handles scheduling & monitoring requests to the Ruby service.

I'd like to send some kind of periodical progress report on the behaviour of the Ruby app back to the scheduler. Perhaps a line every time a discrete operation is completed successfully, such as the moving of one file.

Is this possible with a standard Rack app? FYI I'm using the Camping framework It's not dissimilar from Sinatra, etc. Usually you send the response by just returning from the method:

def get
   @status = 404
   @headers['Content-Type'] = 'application/json'
   return Yajl::Encoder.new.encode({error: 'forbidden', reason: 'I don't like you'})
end

I know that my process is managed by the Rack / web server interface, so I don't know whether it's possible in Rack to send a streaming response without digging into the way the framework works and talks to Rack. Can someone give me some pointers here?

And yes, I know that Ruby isn't a preferred solution for streaming HTTP servers ;)

Upvotes: 0

Views: 645

Answers (2)

dj2
dj2

Reputation: 9618

The Goliath framework handles streaming responses. Take a look at the content_stream example. There is no reason why Ruby isn't a preferred solution for streaming responses, we used Goliath at PostRank for some pretty heavy weight streaming services.

Upvotes: 1

Pablo Castellazzi
Pablo Castellazzi

Reputation: 4184

Sounds like you need a chunked transfer. There is a Rack::Chunked middleware for it too within Rack.

Upvotes: 1

Related Questions