Published: Wednesday, March 22, 2000
Building a Simple Chat Application
Nothing helps grow a community-based Web site than an easy-to-use chat room. In fact, the Internet is really
a gigantic chat room, with millions of people sharing suspended chatting through the form of email or
more lively chatter on a community site's chat application. This article looks at the fundamentals for creating
a chat room application, presenting code to a simple chat application submitted by 4Guys reader Gordon C.
in the earlier ASP Connections Programming Challenge Contest.
The Building Blocks of Chat Rooms
To build a chat room, what are the essential building blocks? What components make up a chat room? Some
method to allow visitors to "log on" is needed. Once a visitor logs on, he or she should be able to post messages
to the chat room, and his or her user name should be remembered throughout the stay on the site.
Chat rooms also need some mechanism to store the latest N messages, and have them displayed for all
visitors to see. Furthermore, chat rooms must constantly refresh the last N messages, so that a visitor
can easily follow the progression of the on-line discussion(s).
In Gordon's chat application (demo it now!) two
frames are used. The top frame stores the last 25 messages, while the bottom frame presents the visitor with
a text box to enter their name and another text box for their message. Every 10 seconds, the top frame is
refreshed, reflecting the latest 25 messages. Application variables are used to store these 25 messages.
Application variables are a good choice for a simple chatting application. First off, they are super-fast, which
is always good; second, they are global to all visitors on the Web site, which is good, allowing all visitors
to participate in one communal discussion.
Unfortunately, the messages are stateless. That is, if you post a message, 25 messages later, your message is
no longer on the chat board - it has vaporized into thin air. If saving a log of the chat is important, a
database could be used to record all comments made. This approach, of course, would be slower than using just
application variables.
To learn more about application variables, take a moment to read
Application Variables. To learn more about
state maintenance, including application variables, be sure to read the free sample chapter from
Sams Teach Yourself Active Server Pages 3.0 in 21 Days: Maintaining
Persistent Information on the Web.
To learn more about practical uses of application variables, be sure to read:
Holding the 25 Past Messages
As mentioned earlier, Gordon's simple chat application uses application variables to store the last 25
messages. Rather than using 25 string application variables, Gordon uses four application arrays, each with
25 elements. These arrays - which store information on the User, Time, IP, and Message of a given "sentence" uttered
in the chat room - are reshuffled a bit each time a new post is made by a user. Each time a new post is made,
the following code is used to add the new message to the application arrays:
Application.lock
if Application("Tplace") => Application("Tplaces") then
Application("Tplace")=0
else
Application("Tplace") = Application("Tplace") + 1
end if
TempArray1=Application("User")
TempArray2=Application("timesaid")
TempArray3=Application("ip")
TempArray4=Application("Message")
if instr(1,mid(message,1,3),"cls") = 0 then
TempArray4(Application("Tplace")) = message
else
redim TempArray1(Application("Tplaces"))
redim TempArray2(Application("Tplaces"))
redim TempArray3(Application("Tplaces"))
redim TempArray4(Application("Tplaces"))
TempArray4(Application("Tplace")) = "Page Cleared by : " & username
end if
TempArray1(Application("Tplace")) = username
TempArray2(Application("Tplace")) = timesaid
TempArray3(Application("Tplace")) = ip
Application("User")=TempArray1
Application("timesaid")=TempArray2
Application("ip")=TempArray3
Application("message")=TempArray4
Application.unlock
|
Don't worry, the full code for the message posting is
available.
Refreshing the Display
Recall that in the top frame, Gordon displays the last 25 messages. To keep this up-to-date, Gordon employed
the use of a META tag to automatically refresh the page every 10 seconds. This design could be improved upon
slightly by automatically refreshing a particular user's display screen immediately after that user has posted
a message. Furthermore, a hidden frame and some tricky ASP/JavaScript could be used to determine whether or
not the display frame needs to be refreshed.
The following ASP snippet outputs the META tag, setting up the ten second delay for the display page:
<%="<META HTTP-EQUIV=""refresh"" content=""5; url=display.asp"">"%>
|
That about wraps up this introduction to creating chat applications using ASP! Hope you found this article
interesting and useful. Gordon's full source code is available at the bottom of the article. Also, don't
forget to try out the on-line demo! If you enjoy
on-line chatting, be sure to check out our WebChat! often!
Attachments
Download the Chat Application in ZIP format
Try the on-line chat demo