Pages

Saturday, February 18, 2012

ColdFusion WebSocket Part1:Getting Started

ColdFusion Websockets?What can I do with it?
                 You can write real-time Web applications and handle data with high efficiency but less complexity. Consider the case of a simple collaboration app that you use to chat. It is a simple task to build it using ColdFusion WebSocket. Yes, you could have done this without WebSocket, but that demands complex architecture and some hacks. Likewise you can create  bidding apps, a stock ticker etc in very short amount of time with ColdFusion WebSockets.

But how is it different?
    I know, you are used to HTTP. WebSocket is different from normal HTTP request response way of communication. In simple terms, if you want any data from your app server you have to send an http request in the form of form submit or some page request,but with  WebSocket you can keep receiving data from the app server without any action. Yes, I mean no action. In fact, you can have a two-way communication between client and server without page reloads.

So how do I implement it?
     You need to have ColdFusion 10 to make use of WebSocket. If you have not  installed it yet, get it from here .
Here is a very simple websocket channel implementation with  minimal lines of code

Step1:  Specify the channelname in  Application.cfc file
            This is absolutely mandatory if you need to establish websocket channel communication.
So your simplest Application.cfc will have the below code.
component
{
   this.name="wsdemoapp";
   this.wschannels=[{name="stocks"}];
}
Note that the above code is in ColdFusion Script syntax. We have specfied the name of the websocket channel as stocks

Step2: Use the new cfwebsocket tag to establish connection from your cfm page to your channel

<cfwebsocket name="mycfwebsocketobject"  onmessage="mymessagehandler" subscribeto="stocks" >

Here we have given mycfwebsocketobject as the value for name attribute, which will create a reference to a javascript websocket object.What this implies is that you can use mycfwebsocketobject as a Javascript object inside javascript code .This object has certain methods associated with it which can be used to  perform various operations over websocket.

Also note that in the above code sample I have specified mymessagehandler as the value of onmessage attribute. mymessagehandler should be a Javascript method .This is the method that will recieve all communications over the channel.

Step3:Define the message handler
<script type="text/JavaScript">
   function mymessagehandler(messageobj)
    {
         //Converting the JS object to a string and display  in "myDiv"
           var message = ColdFusion.JSON.encode(messageobj);
           var txt=document.getElementById("myDiv");
           txt.innerHTML +=message  +"<br>";
 }

</script>
Don't forget include a div with id "myDiv"  in your cfm page to display the messages .

Step4:Run your cfm page.
If you see a string of below format in your cfm page the WebsSocket Connection is successful

{"clientid":41244233,"ns":"coldfusion.websocket.channels","reqType":"welcome","code":0,"type":"response","msg":"ok"}

You can write application specific logic in your message handler to display the messages coming over websockets  in appropriate way.You can download the example described here.

This example just establishes a connection of websocket .For more on how to transfer data, keep watching for more posts. In the next few blogs, I will explain more advanced scenarios.
Happy playing with ColdFusion 10!!!

For more on how to publish messages check out  ColdFusion WebSocket Part2: How do I publish Message? 

7 comments:

  1. Nice article Evelin!!! Thanks a lot. Subrata

    ReplyDelete
  2. very interesting. looking forward to the next post

    ReplyDelete
  3. Hi Evelin... very nice article... just wondering what is the technology used behind the scenes for this? Anything like message queues?...

    ReplyDelete
  4. Very nice Post. Thanks for sharing useful information about coldfusion web services

    ReplyDelete
  5. Really informative one with simple explanation. Thank you.

    ReplyDelete
  6. That you are undoubtedly fatigued using overall look projects. Finish you've still got the vitality for you to unpack the particular card board containers storage containers bins storage area space area position
    Movers and Packers in Mumbai
    Movers and Packers in Hyderabad
    Movers and Packers in Bangalore

    ReplyDelete
  7. Not a way! And so, item each and every package efficiently. That will allow you to unpack card board containers storage containers bins storage area space area position place position storage containers
    Movers and Packers in Chennai
    Movers and Packers in Delhi
    Movers and Packers in Pune
    Movers and Packers in Gurgaon

    ReplyDelete