Event Machine: How to get file downloads?

This is a bit unusual for a regular Ruby / Rails application. I am creating an application that mainly runs on top of Event Machine. There are several HTML files that are submitted from WEBrick, but most applications run on the client side with javascript, with a web connection to my Event Machine application. I need to be able to receive files and save them locally. How can I do that?

Update: If you're interested, here is the link to the source code .

+4
source share
3 answers

First of all, how to create a simple file upload using Sinatra: Downloading a file using Sinatra

So now, to start your thin web server, you can do this in your code:

class MyWebApp < Sinatra::Base # here goes the sinatra app code post '/something' do # ... end end EM::run do Thin::Server.start('0.0.0.0', 8000) do map('/'){ run MyWebApp.new } end end 

thin uses eventmachine internally, I suppose webrick uses streams, but to be honest, I never looked at it.

You should use apache or nginx in front of your ruby ​​process, at least to download the file (I think the web ports will not work through it). I can enable a simple nginx configuration if you need to (just need to find it on my xD drive).

Edit: Another solution is to use goliath as a web server, you have an example: https://github.com/postrank-labs/goliath/blob/master/examples/async_upload.rb If you do not need to show the download progress, you should stay with sinatra + nginx / apache.

+1
source

Please see this project on GitHub: http://www.github.com/igrigorik/em-websocket

The code is on the following link (the code in server.rb may be the starting point), the only caveat is the use of Sinatra, but you can (hopefully) easily adapt it for WEBrick: http://www.github.com/thirtysixthspan/waterunderice

+1
source

If you need downloads to connect via a web socket and therefore cannot use standard Rails controllers for this, use EventMachine.defer to create a new stream to control file downloads without blocking your reactor.

0
source

Source: https://habr.com/ru/post/1395335/


All Articles