ColdFusion 8: Extending Simple Chat Application : Multi Room support with SubTopics

This new sample illustrates how to take advantage of subtopics when using Messaging.

With the default setup, to send messages to a specific room we would have to handle all messages from the default destination at the client side and populate the right room with the new message or create 1 destination (at runtime) per room and each destination would handle their own messages. The first approach is not the best since we would get all messages from all rooms which would consume unnecessary bandwidth with useless data. While the second approach can solve our problems it does not work in FDS 2.0.1 since it does not support runtime configurations.

There is a third option which is the use of subtopics. When assigning the subtopic property to a consumer/producer, you are sharing the same destination but just want to send a message to a subset of the connected clients to that destination. In this sample, the room name is the subtopic so each client connected to a specific subtopic will only get messages from that room.

Installation: in the messaging-config.xml file ( under { cf-webroot }/WEB-INF/Flex folder ) you just have to add this lines to your ColdFusionGateway inside the properties tags.

<server>
<allow-subtopics>true</allow-subtopics>
<subtopic-separator>.</subtopic-separator>
</server>

Also replace the previous messagingListener.cfc with this new one. Restart the ColdFusion server afterwards.

Running the example:

Open the samples2.mxml and run it twice. In each window, create 2 rooms ( A and B) and align them side by side so you can see them working. In both windows fill the username and the message in room A, then press send message. You'll see that only room A got those messages.

How does it work:

When you create a new room, an event listener is created to process send messages

line 30 : room.addEventListener( 'RoomMessageEvent' , sendMessage );

and when it's created, it subscribes itself to the default destination but assigning the room name as the targeted subtopic.

private function init():void{
consumer.subtopic = roomName;
consumer.subscribe();
}

When you press the send message button, a RoomMessageEvent is dispatched and listened by the main application.

Since a RoomMessageEvent was dispatched, the function sendMessage will be called passing the event as argument. We can then access the room properties by checking the event.target property assign the roomName to the subtopic that we want to send to.

Then the ColdFusion Event Gateway recieves the message and sends it back the exact same message.

<cfloop collection="#cfevent.data.headers#" item="header">
<cfset msg.headers[header] = cfevent.data.headers[header]>
</cfloop>

This will copy all headers from the original message to the new message, so it will be delivered to the same subtopic. Note that I used [] notation to preserve the case sensitivity. In CF there is no problem but since the headers are flex related properties, the case will matter.

Then the corresponding consumer listening for that subtopic processes the message and append it to the variable.

private function handleMessage(e:MessageEvent):void{
var body :Object = e.message.body;
chatMessage += body.user + ' says:' + body.msg + '\n';
}

You can get the files here.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Excellent example, worked straight away.

Thanks mate, you are a star. and thanks for the reply on my other post, will give it a try.

Thanks and regards

Philip
# Posted By Philip Bedi | 3/18/08 7:50 AM
Hi ya,

You were right, it did work straight away in BlazeDS.

Thanks

philip
# Posted By Philip Bedi | 3/18/08 8:22 AM
I"m testing this examples but this error ocurred

Unable to send message to CF Gateway 'flexmessaging': You have specified a gateway ID [flexmessaging] that does not exist or is not currently running.


How to resolve ?
# Posted By Marcio | 4/26/08 10:07 PM
With the release of CF8 + LCDS 2.5.1/2.6, would it be possible to do the 2nd approach? That is, is it possible to create destinations at runtime? I've seen examples on how to do this in Java but none for Coldfusion. Any times would be appreciated. Thanks.
# Posted By Sky | 8/11/08 11:48 AM
Sky,

you can try to check this post http://www.onflexwithcf.org/index.cfm?mode=entry&a... , this will allow you to create destinations at runtime.
# Posted By João Fernandes | 8/11/08 12:11 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001. Design by dcarter.