4

This is a little out of the ordinary for a normal Ruby/Rails application. I am building an application that mostly runs on top of Event Machine. There are a few HTML files that are served up from WEBrick, but the majority of the application runs client-side with javascript, with a Web Socket connection to my Event Machine application. I need to be able to accept file uploads and store them locally. How can this be done?

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

Andrew
  • 227,796
  • 193
  • 515
  • 708
  • Look at the specification for `multipart/form-data` in HTTP and copy that approach. – millimoose Feb 08 '12 at 01:09
  • When googling for "HTTP file upload". E.g. here:http://www.faqs.org/rfcs/rfc1867.html and more informally, here: http://www.vivtek.com/rfc1867.html – millimoose Feb 08 '12 at 23:27
  • This will help : https://github.com/igrigorik/em-websocket The code on the following link (server.rb) might help, the only caveat is its using Sinatra but you can (hopefully) easily adapt it for WEBrick : https://github.com/thirtysixthspan/waterunderice – Anand Shah Feb 15 '12 at 12:11
  • is there a reason you are using Webrick to serve pages with an eventmachine reactor running ? As for the question would simple HTTP upload be enough or are you looking for something else ? – Schmurfy Feb 15 '12 at 15:42
  • @Schmurfy the reason I'm using WEBrick is because I'm new to Ruby/EventMachine/etc and didn't know of a better way to do it. If you know a better way, please help! =] Simple HTTP upload would be fine. – Andrew Feb 15 '12 at 20:35
  • @Andrew: have you managed to look at the file server.rb on the link I posted earlier? – Anand Shah Feb 15 '12 at 21:39
  • @Anand yeah thanks for that, you should post it as an answer so I can up-vote it. =] – Andrew Feb 16 '12 at 02:35

3 Answers3

1

Please have a look at this project on GitHub : http://www.github.com/igrigorik/em-websocket

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

Anand Shah
  • 14,575
  • 16
  • 72
  • 110
1

First here is how to build a simple file upload with sinatra: File upload with Sinatra

So now to run your web server with thin 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 threads but honestly I never really looked into it.

You should use apache or nginx in front of your ruby process at least for the file upload (I think websockets won't work through it). I can include a simple nginx config if you need (just need to find it on my disk xD).

Edit: Another solution is to use goliath as web server, you have an example here: https://github.com/postrank-labs/goliath/blob/master/examples/async_upload.rb If you don't need to display upload progress you should stay with sinatra+nginx/apache.

Community
  • 1
  • 1
Schmurfy
  • 1,715
  • 9
  • 17
0

If you need the upload to come through a web socket connection, and thus can't use standard Rails controllers to do this, use EventMachine.defer to spawn a new thread to manage the file upload without blocking your reactor.

Joshua
  • 5,336
  • 1
  • 28
  • 42
  • Could you elaborate a bit more? Obviously I can't use Rails controllers because I'm not using Rails. But how do I tell EventMachine that I want it to "manage file uploads"? – Andrew Feb 15 '12 at 21:06