Ian Hobson
6/22/2005 9:41:00 PM
In message <Xns967D82805B0FBcanimal@68.12.19.6>, Matt C
<canimal@invalid.com> writes
>New to Ruby, and haven't tried anything like this before.
>
>I'd like to write a web page that acts like a "chat room". (Actually,
>I'd like to write a game that incorporates the chat room idea, but first
>things first.) Nothing unusual here: a big window that shows what other
>people have typed, and a small textbox below where you can type your own
>messages. This can be done with ajax, there are some examples on the web
>(but not Ruby ones, that I found anyway).
>
>I'm not sure what the best way to share info between the different web
>users is. There are several possibilities I can think of: use a text
>file, use a database, write a daemon, use memcache.
>
>I'm leaning toward using a database at this point because 1) there won't
>be issues finding hosting if I use a db, and 2) I have experience with
>databases, unlike daemons and memcache. Still, it seems like overkill to
>go in and out of a database when all I really want is shared access to an
>array.
>
>Does anybody have a better idea, or other suggestions?
>
When I wrote one, this is what I did - using a database.
I set up two conversations, using a foreground and background window.
The foreground window loaded, opened the background window, and
presented a simple form to the user. On clicking send, the content was
POSTed using javascript. The server added the line to the conversation,
and returned a null reply (so the window remained without the users
input!).
The background window asked for the conversation from message X, using
GET.
When this came back it was added to the foreground panel using
javascript. And the background window asked for the next (from X+1). It
could receive multiple lines.
The server handling the requests would sleep for (iirc) 2 seconds, and
look, sleep 2 and look, before finally giving a "there are 0 lines to
add" reply every 10 seconds or so. If it found lines to add these were
returned as soon as seen.
Result was user updates within at most 2 seconds, plus transit times -
it felt responsive.
Resulting server load was remarkably light. A conversation with 50
scripts all chatting 1 line per second, hardly made any impression on
the CPU usage on my 500MHZ firewall acting as server. It filled log
files though.
If I did it again, I would use an ipanel and httprequest, but the same
method, thus avoiding pop-up blockers which sometimes caused me a
problem.
You cannot keep the thread running in the server, indefinitely for two
reasons: first your hosting company will kill it after a few minutes as
a runaway, and second, the user's browser will give up waiting if there
is no response.
Hope this give you some ideas.
Regards
Ian
--
Ian - posting to a Newsgroup. Please remove everything to reply.