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
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.
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
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?
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
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?
Nice article Evelin!!! Thanks a lot. Subrata
Replyvery interesting. looking forward to the next post
ReplyHi Evelin... very nice article... just wondering what is the technology used behind the scenes for this? Anything like message queues?...
ReplyVery nice Post. Thanks for sharing useful information about coldfusion web services
ReplyReally informative one with simple explanation. Thank you.
ReplyCold Fusion Online Training, ONLINE TRAINING – IT SUPPORT – CORPORATE TRAINING http://www.21cssindia.com/courses/cold-fusion-online-training-92.html The 21st Century Software Solutions of India offers one of the Largest conglomerations of Software Training, IT Support, Corporate Training institute in India - +919000444287 - +917386622889 - Visakhapatnam,Hyderabad Cold Fusion Online Training, Cold Fusion Training, Cold Fusion, Cold Fusion Online Training| Cold Fusion Training| Cold Fusion| Courses at 21st Century Software Solutions
ReplyTalend Online Training -Hyperion Online Training - IBM Unica Online Training - Siteminder Online Training - SharePoint Online Training - Informatica Online Training - SalesForce Online Training - Many more… | Call Us +917386622889 - +919000444287 - [email protected]
Visit: http://www.21cssindia.com/courses.html