By: Chris A.

Creating a Chat System in ColdFusion without using a database!

Being a ColdFusion Developer a little over a year now [Ex-ASP Developer], I try to find ways to make my existing applications faster and lighter at the same time. I originally created this chat room back in the year 2000 in ASP using a Microsoft Access Database. Since I have been learning ColdFusion, I naturally wanted to change all my previous ASP applications to ColdFusion code. This is the end-result of the chat room from ASP to ColdFusion. (I also wanted to thank Pablo and everyone at EasyCFM, even though I have not yet signed up for a forum account, I use the site and forum often to help me learn, so thank all of you!) You can download all the code for this tutorial here.

This tutorial will have a total of 6 ColdFusion pages.

  • Application.cfm
  • Index.cfm
  • Logout.cfm
  • Messages.cfm
  • PostMessages.cfm
  • User_List.cfm

I will give a brief explanation of each page so you can understand the application a little better. The actual code is directly below the explanations!

APPLICATION.CFM
This is the page were we set the Application and Session variables that are used throughout the application.
INDEX.CFM
This is the page the loads either the login page or the chat window frames.
LOGOUT.CFM
This page will log the user out and alert everyone in the chat room about it.
MESSAGES.CFM
This is the page that displays the chat conversations.
POSTMESSAGES.CFM
This is the page you can use to post your message to the room.
USER_LIST.CFM
This page shows you who is currently in the chat room!

The following is the actual code that the files mentioned above contain!

APPLICATION.CFM
<cfsetting enablecfoutputonly="Yes">
<!--- ^^^^^^^^^^^^^^^^
         Define a new application
        ^^^^^^^^^^^^^^^^ --->

<cfapplication name="ChatRoom"
                     clientmanagement=
"Yes"
                     sessionmanagement=
"Yes"
                     sessiontimeout=
"#CreateTimeSpan(0,0,15,0)#"
                     applicationtimeout=
"#CreateTimeSpan(0,2,0,0)#">

<!--- ^^^^^^^^^^^^^^^
        Define default variables
        ^^^^^^^^^^^^^^^ --->

<cfparam name="Application.UserList" default="">
<cfparam name=
"Application.MessageLog" default="">
<cfset Application.Title =
"My Chat Room!">

<!--- ^^^^^^^^^^^^^^^^^
        First define default session
        ^^^^^^^^^^^^^^^^^ --->

<cfparam name="session.MyUsername" default="">
<cfparam name=
"session.goodUser" default="No">

<!--- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        Now see if the user has logged themselves in 
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --->
<cfif IsDefined("FORM.MyUsername") and FORM.MyUsername neq "">
        <!--- ^^^^^^^^^^^^^^^^^^^^^^^
                First see if the username is available 
                ^^^^^^^^^^^^^^^^^^^^^^^ --->
        <cfset UserOkToUse = ListFind(Application.UserList, FORM.MyUsername, "#chr(9)#")>

        <!--- ^^^^^^^^^^^^^
                Username ok to use
                ^^^^^^^^^^^^^ --->
        <cfif UserOkToUse eq 0>
                <cfset session.MyUsername = FORM.MyUsername>
                <cfset session.goodUser =
"Yes">
                <!--- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                         Now add user to list of users
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --->
                <cfset Application.UserList = ListAppend(Application.UserList, FORM.MyUsername, "#chr(9)#")>
                <cfset Application.MessageLog =
"<font color=Green><b>#session.MyUserName# Logged In at #DateFormat(now())# #TimeFormat(now())#</b></font><br>" & Application.MessageLog>
        <cfelse>
                <cfset UserMessage =
"Username taken, try another!">
        </cfif>
</cfif>
<cfsetting enablecfoutputonly=
"No">
INDEX.CFM
<cfoutput>
<html>
  <head>
     <title>
#Application.Title#</title>
  </head>


<cfif session.goodUser eq "Yes"
  <frameset rows="64,*">
     <frame name=
"postmessage" scrolling="no" noresize src="postmessage.cfm">
      <frameset cols=
"150,*">
          <frame name=
"users" target="main" src="user_list.cfm">
          <frame name=
"main" src="messages.cfm">
      </frameset>
      <noframes>

         <body>
            <p>This page uses frames, but your browser doesn't support them.</p>
         </body>

      </noframes>
  </frameset>
<cfelse>

  <body>
    <form action="index.cfm" method="post">
    <cfif IsDefined("UserMessage")>
       #UserMessage#
    </cfif>
    <table width="300" border="0">
      <tr>
          <td width=
"50%">Screen Name:</td>
          <td width=
"50%"><input type="text" name="MyUsername" value=""></td>
      </tr>
      <tr>
          <td width=
"100%" colspan="2"><input type="submit" name="LogMeIn" value="Log In"></td>
      </tr>
    </table>

    </form>
  </body>
</cfif>

</html>
</cfoutput>
LOGOUT.CFM
<!--- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        LET EVERYONE KNOW THAT THIS USER LOGGED OUT
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --->

<cfset Application.MessageLog = "<font color=red><b>#session.MyUserName# Logged Out at #DateFormat(now())# #TimeFormat(now())#</b></font><br>" & Application.MessageLog>
<!--- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        TAKE USER OUT OF THE LIST OF LOGGED IN USERS
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --->

<cfset Application.UserList = ListDeleteAt(Application.UserList, ListFindNoCase(Application.UserList, session.MyUsername, "#chr(9)#"), "#chr(9)#")>
<!--- ^^^^^^^^^^^^^
         LOG THE USER OUT
        ^^^^^^^^^^^^^ --->

<cfset session.MyUsername = "">
<cfset session.goodUser =
"No">
<!--- ^^^^^^^^^^^^^^^^^^^^^^^^^
        TAKE USER BACK TO THE LOGIN PAGE
        ^^^^^^^^^^^^^^^^^^^^^^^^^ --->

<cflocation url="index.cfm" addtoken="Yes">
MESSAGES.CFM
<html>
  <head>
     <title>
Message Log</title>
     <META HTTP-EQUIV=REFRESH CONTENT=
"5">
  </head>


  <body>

  <cfoutput>
     #Application.MessageLog#
  </cfoutput>

  </body>
</html>
POSTMESSAGES.CFM
<!--- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        If a new message if posted, put into the application variable so everyone can see it
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --->

<cfif IsDefined("FORM.new_message") and FORM.new_message IS NOT "">
     <cfset Application.MessageLog =
"<b>#session.MyUserName#</b>: #FORM.new_message#<br>" & Application.MessageLog>
</cfif>

<!--- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         DISPLAY THE FORM THAT ALLOWS NEW MESSAGE TO BE ENTERED
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --->

<html>
  <head>
     <title>
Post New Message</title>
  </head>

  <body bgcolor=
"#000000">

  <form action="postmessage.cfm" method="post">
  <table width="100%" border="0">
    <tr>
       <td width=
"100%"><input type="text" name="new_message" value="" size="50">
                                    <input type=
"submit" name="PostIt" value="Post Message">
                                    <a href="logout.cfm" target="_top">Logout</a></td>
   </tr>
  </table>

</form>

</body>
</html>
USER_LIST.CFM
<html>
  <head>
    <title>
Logged In Users</title>
  </head>

  <body>


  <cfoutput>
   <form>
    <strong>Logged In Users:</strong><BR>
    <ul>
    <cfloop list="#Application.UserList#" index="UserName" delimiters="#chr(9)#">
        <li>#UserName#</li>
    </cfloop>
    </ul>
    <input type="submit" name="Refresh" value="Refresh User List">
   </form>

  </cfoutput>

</body>
</html>

That's it, if you find anything wrong with the example or a better way to do that chat, let me know. I'm always looking for ways to improve my applications.
- chris

About This Tutorial
Author: Chris A.
Skill Level: Intermediate 
 
 
 
Platforms Tested: CF5,CFMX
Total Views: 226,772
Submission Date: May 10, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 1
Discuss This Tutorial
  • I have been through many tutorials, you have a natural gift for teaching. The code was well written and the results were ease of use. Thank you again "I see the light"!

  • Thanks,I am keen to use it in my web site but the probs is that it has only public chatting and i want use private chatting too. thanks!

  • Thanks, I'll probably use this on my little website just to talk to friends and whatnot. Pretty cool of you to post.

  • great tutorial! when the page refreshes, it naturally returns to the top of the page. This is fine unless you are trying to read older posts that are currently off the page. Is there a way to have it refresh without starting at the top? thanks!

  • For the most part I removed all the chr(9) delimiter's and just went with the default comma's - if you keep the chr(9) delimiter you need to add it accordingly above. Likewise you should then not allow usernames with comma's. Hope this helps Troy

  • First to get rid of the annoying 'click' sound in IE when the messages.cfm frame refreshes, replace the Meta refresh tag with a bit of javascript... Second add a bit of filtering - add two lines to application.cfm... Next add one line in postmessage.cfm after the Third, when the user enters a chat statement put the focus back into the submit box. In postmessage.cfm... (I named the form 'post') The final big thing is the garbage collection of users who didn't specifically logout. My idea here was to constantly tell the Application that my session was still active by resetting a counter every reload of the message pane. It works like this... Add a new array that coresponds to the username array containing a counter for each user. When the page reloads set my counter to 0 and increment my neighbor's counter. If someone's counter gets to 5 or so, remove them and their counter from the list. There are several things to add in Application.cfm, message.cfm, and logout.cfm Application.cfm and right after messages.cfm logout.cfm Add this BEFORE the

  • Finally solved my onunload issue. Interesting story...kind of. The original Google popup blocker, which I have, blocks all onunload events in IE, regardless of whether or not it's being used to launch a popup. Netscape 7 is developed to block onunload="window.open()" events to stop popups. So, it became obvious that popping up a window to run a script to log someone off wouldn't work (like in the old days). So, I decided to use onunload in Netscape to simply change the parent doc url to log the person off when the chat window is closed, which worked, but amazingly not in IE. Still though, onunload is blocked in IE 5.5+ if you have google popup blocker even if you are simply changing an url of an existing window. However, google doesn't block onBeforeUnload, and Netscape doesn't recognize that one. Anyway, just put them both in the body tag, and you'll get results in both browsers with popup blockers. PS. windows.onclose() was causing the action to occur onload when loading pages in a framed set of windows, not unloading for some reason.

  • What if someone decides to simply close the browser rather than click logout? How do you remove them from the "chatter list" in a timely manner? Note: onUnload won't work in Netscape when closing the browser. I'm stumped on this one. Thanks.

  • Dear Sir; I need your help to provide me about how i can use Application.cfm page that include a design for the whole web distributed in web pages, inspite of that i was do the steps for the application and calling the page that contain the frameset, but i found that the page call one frame and ignore the other where the calling frame is saved as *.html at the first but i found no calling to the whole content of the page. The Application.cfm page should include 1- Three Frameset 2- frame one could design the look of the site to distributed. Thank for Help B. Rayyan Arab American University West Bank - Israel

Advertisement

Sponsored By...
Mobile App Development (IOS, Android, Cordova, Phonegap, Objective-C, Java) - Austin, Texas Mobile Apps - Touch512, LLC.