Home > ajax, ColdFusion, Railo > Simple chat app using jQuery and ColdFusion

Simple chat app using jQuery and ColdFusion


Way back in the day I was working on a jQuery game I called CFMud. As the name alludes to, it was to be a multi-user dungeon that would allow you to roam around “the world” and gain experience. One of the key components of the UI was the chat interface. I was just getting in to jQuery and ajax at the time so I never really worked all the kinks out of the system then life intervened and I have never picked the project back up (sorry Josh :P ).

Yesterday a co-worker mentioned that she was looking for a simple little chat application so I figured I would give it a shot. I threw together an extremely quick, extremely basic little demo. The application itself is comprised primarily of three pieces: the index.cfm file for the user interface, an application-scoped CFC that acts as the “chat server”. and finally an ajaxProxy.cfc that acts as a facade for remote calls.

To start, just call up the index.cfm you will need to enter a user name to use in the chat window. Enter a name and click on the “Set Name” button. This will then bring up the main chat window and currently updates every five seconds. I have tested it in the main browsers; FireFox, Chrome, and with much swearing, Internet Explorer. For some reason, IE likes to cache remote calls to CFC’s as well. Go figure.

The “chat server” is actually just an array stored in server memory.  Currently it is configured to store 100 messages, it will purge out the oldest message if the current chat log goes above this.  Also, to keep down the amount of data being transferred from the server, each client remembers the message id it received.  When the next call to get messages occurs, it only gets new messages.

If you would like to check it out, I have it available for download here: http://dl.dropbox.com/u/868773/chat.zip.  It is hardly worthy of an RIAForge project, but maybe I will play around with it some more later on.

To see it in action: http://demos.kisdigital.com

Categories: ajax, ColdFusion, Railo
  1. asim
    November 30, 2010 at 12:34 pm | #1

    didn’t work

    Error:
    The local variable variables cannot be declared outside of a function.

    I have tried with CF8 and CF9 with updater 1

    • November 30, 2010 at 12:40 pm | #2

      I haven’t really haven’t updated this code since I uploaded it in March or so. I will take a look at it and get it back in running condition when I have a few minutes. Thanks for the heads up.

  2. George Fallar
    December 2, 2010 at 4:41 pm | #3

    Just remove the ‘var’ from the definitions in chat.cfc and it will work fine.

    • December 2, 2010 at 5:32 pm | #4

      Thanks for taking a look at that George. I have been so busy I haven’t even had time to take a breath, let alone work on side things.

      I appreciate it.

  3. Liakos Kostas
    December 18, 2010 at 8:53 am | #5

    Doesn’t work for me either, i removed the ‘var’ from the definitions in chat.cfc, but now when i set a name, i get a console error which says “uncaught invalid JSON” in jquery-1.4.1.min.

  4. February 18, 2011 at 10:00 am | #7

    I have a question on this chat app. When I send a message of ‘test’ it updates the chat box the first time. The second time you send ‘test’ nothing displays. It seems like you are doing some kind of check to see if the message sent is already there but I don’t see where you do that. I want to send the message to the chat area everytime regardless if it has been sent already. Where do I need to change that in the code?

  5. February 18, 2011 at 10:12 am | #8

    I haven’t had much time to update or play with this code in a long time. If you are playing around with this on Railo 3.2+ or on a CF box you may want to make an adjustment to getMessages() javascript function in index.cfm

    function getMessages(){
    $.ajax({
    url : ‘ajaxProxy.cfc’,
    data : {
    method : ‘getMessages’,
    startID : currID,
    ieFix : Math.random()*999999
    },
    success : function(incoming){
    var out = ”;
    if(incoming.svrStatus == ’0′ && incoming.messages.length){
    for(i=0; i currID){
    out = ‘‘ + incoming.messages[i].userid + ‘ : ‘;
    out += incoming.messages[i].message + nl;
    $win.append(out);
    }
    }
    currID = incoming.lastid;
    setScroll();
    }
    },
    error : function(){
    $win.append(‘Error connecting to remote CFC’);
    setScroll();
    }
    });
    clearTimeout(refreshTimer);
    refreshTimer = window.setTimeout(“getMessages()”, 5000);
    }

    Newer versions of Railo return the json response as mime-type “application/json” where it was being returned as “text/html” when I originally wrote this code. The json.parse() code might have been throwing you off. HTH

  6. February 18, 2011 at 10:14 am | #9

    Also, this has not been tested with jQuery 1.5 either. I know there have been some changes around $.ajax() but I have not looked into it.

  7. February 18, 2011 at 11:28 am | #10

    I’m using Coldfusion 9 and I’m using the same version of jQuery as in your example. How can I debug the chat.cfc? I’m newer to coldfusion and have only really used the cftags and not cfscript. I believe the postMessage function is passing the values correctly but the value is not being added to the array? Any additional help would be greatly appreciated!

  8. February 18, 2011 at 11:35 am | #11

    I found that it works fine in Firefox … the problem shows up in IE…

    • February 18, 2011 at 12:35 pm | #12

      Hmmm.. Is IE showing any javascript errors? I know in some versions JSON.parse() didn’t work correctly which is why I switched up to jQuery 1.4 as soon as I could. They introduced $.parseJSON() in that version which made JSON parsing browser agnostic.

      If you did want to do some diagnostics on the chat component, it is a fairly straight forward process. First just create a blank .cfm page (test.cfm) in the same web root so it will have access to the application variables. The chat component is loaded as application.chat so you just need to reference the postMessage() and getMessages() methods. The postMessage() function requires two parameters; the message to send and the user sending it. You can see the results being returned by dumping the return result:

      <cfdump var=”#application.chat.postMessage(“a message”, “Some Guy”)#”>

      Likewise, you can get the currently queued messages by:

      <cfdump var=”#application.chat.getMessages()#”>

      It does not require any params and it will just dump out the message queue.

      The next step would be to go through Application.cfc and make sure nothing there is mucking things up. I will be glad to help in any way I can.

  9. February 18, 2011 at 4:46 pm | #13

    The chat demo is back up on http://demos.kisdigital.com

  10. July 26, 2011 at 9:30 am | #14

    Really nice……..
    Thank you so much……….

  11. sri
    May 4, 2012 at 5:49 am | #15

    Hi, can you post me code of comments i.e.,

    i need structure of image then message in highlighted form of comments,,,

    plz help me

  12. sri
    May 10, 2012 at 1:26 am | #16
  13. g sidha
    May 25, 2012 at 9:01 am | #17

    Really nice……..

  1. March 27, 2010 at 3:49 am | #1
  2. March 27, 2010 at 5:00 am | #2
  3. March 27, 2010 at 10:35 am | #3
  4. July 25, 2011 at 4:41 pm | #4
  5. July 26, 2011 at 11:03 am | #5

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 364 other followers