ColdFusion 10 Web Socket JavaScript APIs

This post is more than 2 years old.

In my last post, I demonstrated three examples of websockets under ColdFusion 10. One thing I didn't really touch on was the JavaScript API you can use to work with websockets. These functions are available to any file making use of the cfwebsocket tag. They allow you to:

  • Open or close a connection as well as checking if the connection is open (openConnection, closeConnection, isConnectionOpen)
  • Subscribe or unsubscribe to a channel (remember that the cfwebsocket tag can autosubscribe you - that's what my demos did)
  • Authenticate you - technically your back end code will do this, but this helps set up your websocket connection as an authenticated one
  • Get a list of what you're subscribed too (getSubscriptions)
  • Get a count of the people subscribed to a channel (getSubscriptionCount)
  • And finally, invokeAndPublish, which lets you use the websocket connection to run a CFC method.

Each of these functions are asynchronous. The docs clearly say this and anyone who doesn't see this may be a bit slow. (-sigh- yes... I missed it.) So as a simple example, I wanted to add a subscriber count to my chat application. I added the following code within my user registration system (the code run when you tell the application your name):

window.setInterval(function(){ chatWS.getSubscriberCount("chat") },2000);

Remember that "chatWS" is a JavaScript object that is my 'hook' to the websocket.

Each of the JavaScript methods will use your message handler for results. This means your message handler has to be a bit complex. Previously it handled new user arrivals as well as messages. So now I have to add a bit more logic to handle this result. Here's my message hander:

function msgHandler(message){ //Only care about messages if (message.type == "data") { var data = JSON.parse(; if(data.type == "chat") $("#chatlog").append(data.username + " says: " + + "\n"); else $("#chatlog").append( + "\r"); $('#chatlog').scrollTop($('#chatlog')[0].scrollHeight); console.log("Append "; }

if(message.type == "response" && message.reqType == "getSubscriberCount") {


You can visit the demo here:

p.s. When I first tried to use this feature, I didn't realize the calls were asynchronous, even though the docs say this. I also didn't realize my message handler would get the result. I thought - why not simply get a user count after we get each message. So I put the call in the message handler. Take a while guess what this did to my server.

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

Archived Comments

Comment 1 by Don Vawter posted on 2/23/2012 at 8:39 PM

Now just add a handler for the onClose event to unsubscribe the user so the active users count remains accurate (or is this handled automatically?)

Comment 2 by Evelin Varghese posted on 2/23/2012 at 9:54 PM

That was a good experiment to put another websocket call in message handler. :)
@Don: subscriber count is automatically calculated on subscribe and unsusbcribe.Also onclose event is fired on closing the WebSocket Connection.Not on unsubscribe.

Comment 3 by Nuri Cevik posted on 2/27/2012 at 10:29 PM

Hi Ray, throwing error

Comment 4 by Raymond Camden posted on 2/27/2012 at 10:30 PM

Hey Nuri - I had to remove CF10 from the server due to a bug with IIS (CF10 is in beta after all ;). I'll hopefully be up and running soon. If you want a copy of the code, let me know, I'll gladly put it up.