Anjorin asks:
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.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.
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.
Archived Comments
Adrock posted a demo using LCDS - http://www.adrocknaphobia.c...
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.
@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.
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.
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.
@raul Most likely the flash movie needs to have wmode set to transparent or opaque.
@JF - no worry - and I should have mentioned that option anyway (like I pushed him to Flex/LDS)
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
You can do this with Ajax and a JMS gateway as well, if you don't want to use LCDS.
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