Could any one recommend a robust TCP server (like Node.js for JavaScript) for Ruby? I understand that there is a TCPServer class available for Ruby but I need something that is more robust because I don't want to have to write code to deal with multiple clients and multi-threads etc. Is there any library/framework for Ruby TCP server that is sort of like Node.js?
-
Why is it necessary to have your server run under Ruby? There are many different ways to run Ruby scripts as CGIs or as services with nginx and Apache, and, as much as I like Ruby, they'll run circles around an interpreted language. – the Tin Man Dec 17 '12 at 23:39
-
No it's not necessary to have my server run under Ruby. Can you recommend an alternative? – Bryan Dec 18 '12 at 00:08
-
Both nginx and Apache are robust and standard servers. Passenger works with either to help with hosting the app underneath them. Or, see my answer. – the Tin Man Dec 18 '12 at 14:10
2 Answers
It sounds like you're wanting EventMachine or Celluloid. EM is event-driven concurrency, which operates very similarly to node.js. Celluloid is more traditional multithreading.
If you're using MRI, I'd recommend looking at EM first, due to limitations in MRI Ruby's concurrency model. If you're on JRuby, then Celluloid may be the right solution.

- 61,439
- 10
- 123
- 137
Take a look at Sinatra, in particular their "README". It's quite easy to use, handles multiple clients and multi-threads and is easy to set up.
As they show on the front page of their site, put this in a file called "hi.rb":
require 'sinatra'
get '/hi' do
"Hello World!"
end
Then, at the command-line type:
gem install sinatra
ruby hi.rb
You'll be off and running. If you install Thin using gem install thin
, Sinatra will use it as its underlying HTTPd, and you'll gain Event Machine's underpinnings.
Sinatra is the fastest and simplest way to get something on the web in Ruby that I know, and it's quite robust. For normal in-house use it's awesome.
As a next step above Sinatra, look at Padrino. It's built on-top of Sinatra but is a bit more Rails like.
I'll also recommend looking at HAML for generating your HTML content. It's a great tool.

- 158,662
- 42
- 215
- 303
-
I don't think they'd be generating html or accepting requests like a normal REST client if this were a TCP server... maybe you missed that part. – courtsimas Jul 31 '17 at 18:33