Ask a Jedi: CFWINDOW for Messaging?

This post is more than 2 years old.

Anjorin asks:

Please am trying do something and i can get the logic. i am working on small site that include an instant messaging section like the one on facebook. i am trying to use cfwindow, but the problem am having is how to have a listener that listens for new messages. Is it possible to have cfwindow call a function recurrently.

Well first off - using something like Flex and LiveCycle Data Services would make this issue trivial. I assume you can't go that route though. If you want to use a 100% pure Ajax solution, and you want to stick with CF8 (and not use jQuery), then you can do this with polling.

By that I mean you need to have the client perform a request to the server every N seconds or so. This is done pretty easily with the setInterval function. Consider the following:

<html>

<head> <script> function pinger() { ColdFusion.navigate('pinger.cfm','mydiv'); } </script> </head>

<body onLoad="setInterval(pinger,4000)">

<h2>Hello World</h2>

<cfdiv id="mydiv" /> </body> </html>

I've set up the client to run a function, pinger, every 4 seconds. You probably want to make that 30 or even 60. The file you hit, in my case, pinger.cfm, would be responsible for checking to see if a message exists for the user. Here is some simple code I used:

<cfif randRange(1,10) lt 5> <script> ColdFusion.Window.create('win#ranrange(1,999)#','Message','message.cfm?m=x',{center:true,closable:true}); </script> </cfif>

It basically says: If a random number from 1 to 10 is less than 5, make a new window. I named the window dynamically, but you should use another method to give it a truly unique name. I then point it to message.cfm. This is the file that will load the message for the user. The code I used was just this:

<cfparam name="url.m"> <cfoutput>Message #url.m#</cfoutput>

In a real example this would use the database (Transfer FTW) to grab the relevant message, do security checks, etc.

Note that in order for this to work now, back in my first file I have to add:

<cfajaximport tags="cfwindow">

This simply warns ColdFusion to load what it needs for CFWINDOW support.

Any way, the general idea is to run an interval on the client that will poll the server for new messages. Just remember to be gentle with that duration, and I'll say it again - Flex+LDS allows for real communication and would be your best bet.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by todd sharp posted on 6/6/2008 at 6:41 PM

Adrock posted a demo using LCDS - http://www.adrocknaphobia.c...

Comment 2 by João Fernandes posted on 6/6/2008 at 6:43 PM

Ray, there is always the possibility to use AJAX Data Services which will provide messaging support for AJAX clients. If LCDS price is a problem, messaging is also available with BlazeDS.
The advantage with BlazeDS/LCDS is that you don't have to worry about how to implement the full polling system.

Comment 3 by Raymond Camden posted on 6/6/2008 at 6:44 PM

@JF: From what I understand, and please correct me if wrong, the Ajax DS _still_ uses Flash. Right? I mentioned in the blog post that we were assuming 100% Ajax.

Comment 4 by Raul Riera posted on 6/6/2008 at 6:56 PM

Currently CFWINDOW is beign displayed BEHIND flash movies on Windows IE 7 and Windows Fireforks. Why is this happening? On IE6 or MAC it shows fine.

Comment 5 by João Fernandes posted on 6/6/2008 at 6:57 PM

sorry Ray, yes, you are right, it needs a small invisible swf which does the bridge between them. Sorry, missed the 100% ajax solution. My bad.

Comment 6 by joshua cyr posted on 6/6/2008 at 7:00 PM

@raul Most likely the flash movie needs to have wmode set to transparent or opaque.

Comment 7 by Raymond Camden posted on 6/6/2008 at 7:03 PM

@JF - no worry - and I should have mentioned that option anyway (like I pushed him to Flex/LDS)

Comment 8 by Mat Evans posted on 6/6/2008 at 7:52 PM

Isn't this sorta thing what COMET was designed for? Pretty sure the facebook messenger isn't using polling, too inefficient as the messages come through strait away, though I could be wrong.

Mat

Comment 9 by Rob Brooks-Bilson posted on 6/6/2008 at 8:58 PM

You can do this with Ajax and a JMS gateway as well, if you don't want to use LCDS.

Comment 10 by Rob posted on 6/26/2008 at 3:43 AM

Once again, you have come up with the goods! I have used the first section of code as is then added a cfc powered query on the second page that polls new for new messages in my IM application. There is a discussion of other technologies, in you comments, that make the process simpler - why? I can't image the process woul get any simpler/cleaner/obvious than the one yo have provided.

Thanks again
rob