Tuesday, February 28, 2012

ColdFusion WebSocket Part3: Interpreting response

In the previous blog, we discussed how to publish messages. Here, let me explain how to interpret ColdFusion WebSocket responses.

Most of the ColdFusion WebSocket responses are asynchronous, which essentially means that  the response to your WebSocket method calls (such as getSubscriberCount, getSubscriptions, publish, invokeAndPublish, or invoke) come to message handler.
Look at the sample snippet:
var cnt= mywsobj.getSubscriberCount(channelname);
Here, if you expect to get the value of number of subscribers on the channel in the variable "cnt", you are wrong. The response to this WebSocket call comes to your message handler and you have to extract data from there.
Since there are different types of responses and all of them come to the message handler. The application developer has to interpret each response, figure out the operation that triggered, and show the response appropriately.
Let us now try to crack the responses received.
In the examples given in my previous blog post, I used ColdFusion.JSON.encode inside message handler to convert the JavaScript object to a string so that you can see all keys and values of the messageobject.Here we are going to categorize the responses received.
Firstly, let us divide the messages that come to your WebSocket object in two ways depending on the value of code key.
  • If code key is a 0 , it's a successful message.
  • If the code key is -1 or 4001, it's a failure message.
All responses with code 0 will come to the message handler. If you have specified an error handler, all responses with code -1 and 4001 go to the error handler. If  error handler is not specified, all responses go to the message handler.

In the example, we have both message handler and error handler. You can use this code in the message handler. Here messageobj is the argument of message handler function.

if(messageobj.type == "data")
if (messageobj.type == "response") {
case 'welcome' : 
message="Websocket connection Established";

case "subscribe" : 
message="Subscription successfull";

   case "getSubscriberCount" : 
message="No:of subscribers= " + messageobj.subscriberCount;

   case "getSubscriptions" : 
       if (messageobj.channels.length >= 1) {
message=" You have subscribed to "
for (var j = 0; j < messageobj.channels.length; j++)
if(j>0) message +=", ";
message +=   messageobj.channels[j].id;
}else message ="You have not subscribed to any channels"

   case "publish":
       message="Message successfully published";

   case "invokeAndPublish":
       message="Method invoked and message successfully published";

   case "invoke":
        message="Method invoked <br />" ;
message=messageobj.data +"<br />";

   default: message = ColdFusion.JSON.encode(messageobj);

In the code, I have just customized the message that needs to be displayed for each kind of response.
Instead, you can call different JavaScript functions to update your UI controls with appropriate data.
Also, you can use similar code in error handler to handle error situations.
Download this basic code sample which you can use to perform almost all  WebSocket operations. It also displays customized message for each response.

Hope you are more and more convinced that ColdFusion WebSocket is easy and effective. I will try to post some advanced concepts in the next few blogs.


  1. Dude (err dude-ette) - thanks for this. My response handlers were getting messy and it never occurred to me to just use a darn switch statement. Duh.

  2. Very well done. This was super helpful. If you do a part 4 could you address the use of sub-channels?

    1. Yes I will address the use of sub channels in the coming blogs.

  3. Do you know of a way to dig deeper into error handling? I'm having a problem where the websockets aren't invoking CFC's properly however all I get back is an error 4001 and no details other than there was an error invoking the CFC.