3

I am making some game server. As like any other game server does, I think the server should be state-ful. (might be change later, but currently I am looking for state-ful solution)

After playing with Rake some, I decided to find a solution in Ruby. What I am finding is:

  • An embeddable HTTP server library which can be integrated long-running Ruby app. (in-process lib)
  • Should support handling of bare-bone HTTP request/response handling. No decoration, URL dispatching or web-page template. (which I never need)
  • Should offer hard single-threaded mode.
  • Should support HTTPS with self-signed certificate.
  • Reliable, and proven in production.
  • Nice documentation and large community.

I think most similar example is HTTP server integrated into node.js. Basically a thin layer on top of TCP socket.

It doesn't need to support multi-threading. I think I will run separated process for each CPU core and I need fast development, so multithreading is currently what to evade.

I have looked for Rack, and it seems like just a protocol specification, and is not an actual implementation. And it looks like for state-less web-app only. If it is not,please correct me.

So, what kind of options are available for these stuffs in Ruby?

eonil
  • 83,476
  • 81
  • 317
  • 516

3 Answers3

4

After careful re-evaluation of your question I might have found another solution.

Perhaps the only thing you need is a simple TCP socket server. A primitive HTTP 1.0 server can be done with the Ruby core classes like this:

require 'socket'

server = TCPServer.open(80)
loop do
  client = server.accept

  response = "<html>...</html>"
  headers = ["HTTP/1.0 200 OK",
             "Content-Type: text/html",
             "Content-Length: #{response.length}"].join("\r\n")

  client.puts headers
  client.puts "\r\n\r\n"
  client.puts response

  client.close
end

Have a look at these resources:

Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
  • Thanks for another answer. I considered it once, but excluded because I thought it is too early optimization for my current progress. But I feel I will need it very soon. – eonil Mar 04 '13 at 10:12
2

In general if you want to know "What are my options in Ruby regarding [...]" your best resource is The Ruby Toolbox.

The web servers category shows many options. The most popular servers are:

  • Thin
  • Passenger (can be used standalone or with Apache Web Server)
  • Unicorn
  • Mongrel

A few notes on your requirements:

  • It helps if think "The web server embeds my application" rather than "I'll embed the server into my application".
  • In production you'll always need multiple instances of your application, because most Ruby web servers work that way. (Because multithreading in Ruby and its implementations is a tough subject)

Personally I'm using both Thin and Passenger with great success. The combination "Thin during development, Passenger in production" seems to very common, too.

PS: You are not mentioning any web framework. Even if you want to make your application as lightweight as possible, a simple web framework can spare you a lot of boilerplate code. (The Ruby Toolbox: Web App Frameworks)

Community
  • 1
  • 1
Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
  • As I clarified on my question, I am looking for a HTTP server will be integrated into my state-ful, long-running app, not a state-less, short running web app container server. – eonil Mar 03 '13 at 16:56
  • I'm sorry if my answer doesn't help you, but - respectfully - you seem to miss some basics regarding web servers. If your application is stateful it doesn't mean that your server process has to be, too. Also all of the named servers keep your application running, so there is no conflict there, too. – Daniel Rikowski Mar 04 '13 at 09:31
  • Phusion Passenger is designed to run in an external process. If you're looking for an in-process web server then Phusion Passenger is not up for the job. – Hongli Mar 14 '13 at 11:17
0

If you're looking for a simple way to answer some HTTP queries in a Ruby process that isn't primarily a web server (e.g. for debugging), WEBrick seems good to me so far.

https://docs.ruby-lang.org/en/2.1.0/WEBrick.html

require 'webrick'

server = WEBrick::HTTPServer.new(Port: 8000)
server.mount_proc '/some_useful_query' do |req, res|
    res.status = 200
    res.body = 'Hello, world!'
end
Thread.new {
    server.start
}
Alex
  • 18,332
  • 10
  • 49
  • 53