Creating a Chat System in ColdFusion without using a database!

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



All ColdFusion Tutorials By Author: Chris A.