How to keep persistent socket connections?

Steve Hull
Thu, 21 May 2009 17:17:21 -0000

Hello ruby experts!

I''ve been working in Ruby on Rails for awhile now (like 3 months), I
and
have *thoroughly* enjoyed it thus far.

But my team and I have come up against a problem that I don''t think
Rails can address.  I know Ruby itself can implement a multithreaded
socket server, but I don''t know how to make that happen within the
context of rails.

We have 2 distinct (and similar problems) that center around the need to
keep persistent socket connections open (as a socket server, not
client).

For the first, we need to be able to open a socket to a 3rd party server
and keep it open.  Today I''m going to try opening the socket and then
storing the socket in the Rails cache, in order to see if that socket
remains open and useable even while just sitting in the cache (I''m kind
of thinking it won''t).

For the second, we need to build a game server with built-in chat.  The
clients need to be able to open a socket and then send and receive
game-related messages on that socket.  I have done some digging and
found TCPServer class that Ruby provides, which could definitely fit the
bill.  I''ve also seen Gserver and the "basic scrappy little chat
server"
example provided at
http://www.rubyinside.com/advent2006/10-gserver.html.  I''m thinking --
is there any way I could run a Gserver or TCPServer on top of rails?
Have it listen on a different port, but still have all the code inside
be able to access my rails classes/models/etc?  Then I could have a
multithreaded server, but with all the convenience and power of RoR.

Let me know what you folks think.  I''m really looking forward to having
my mind blown by your solutions.  :)

-Steve