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:
Don't worry, the full code for the message posting is available.
Refreshing the Display
The following ASP snippet outputs the META tag, setting up the ten second delay for the display page:
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!