Tim Bennen wrote:
- scale your existing protocol. You could make it work very efficiently
with thousands of concurrent connections using ‘eventmachine’ or one of
the other reactor-pattern libraries. Adding SSL to your existing code is
also pretty easy.
If I would use EventMachine, how would be the architecture? EventMachine
receives the messages and forward it to my ruby scripts? But
EventMachine would replace my TCP server?
Yes, eventmachine would replace your TCP server. You would have to rejig
your application into an event-handling model, rather than a threaded
model. Because there are no threads, it scales very well.
I’m assuming here that the scaling you’re looking for is large numbers
of concurrent TCP connections, more than large numbers of messages per
second. But you can also scale to multiple cores and/or servers by
putting a simple TCP load balancer in front of your server(s). ‘pen’ is
a suitable software load-balancer, or you can buy lots of different
hardware appliances.
And with EventMachine it is
easy to set up a persistent SSL connection?
I’m not sure, you’d have to look for samples in the eventmachine
distribution and/or ask on an EventMachine list. But I would strongly
expect it has been done before.
With a simple TCP socket server like you already have, it’s easy to turn
it into an SSL server. For example, there is code in ruby-ldapserver
which does this (look at startssl in connection.rb and ssl_prepare in
server.rb)
Okay, I could build up an apache webserver with for example phusion
passenger which enables the ssl security and the persistent connection.
And then I use Rack to get the requests and I can send the responses
back to the client. Is that right?
Yes. And for initial testing, you can run the rack app directly under
mongrel or thin or even webrick (rackup will start these servers for
you)
But how do I call the server from the
client? Is it a POST HTTP Request with the JSON message in his body?
Yes. And you’d send back an application/json response with the JSON
message in the body.
Note that HTTP limits you to exactly one response per request. That is,
the server can’t send back asynchronous unsolicited responses to the
client. If you want to do that with HTTP, you’d have to get the client
to send a request and block waiting for a response (aka ‘COMET’) or poll
periodically.