tag:blogger.com,1999:blog-51837592796621073122024-02-07T11:16:35.858-08:00Evey's notesAnonymoushttp://www.blogger.com/profile/15852909723625631148noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-5183759279662107312.post-40711727948071473382012-11-09T01:33:00.003-08:002012-11-09T01:33:31.024-08:00Load Testing & IIS Application poolIf you are doing a load testing to see if load is going to create a crash in your application pool, there is one setting worth checking.<br />
In IIS 7 and 7.5<br />
<br />
<div class="MsoNormal">
Go to Advanced settings for the app pool<o:p></o:p></div>
<div class="MsoNormal">
In Rapid Fail Protection,Enable = true, set
Maximum Failures=1<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In IIS6</div>
<div class="MsoNormal">
Right click on application pool->Properties</div>
<div class="MsoNormal">
Click on Health tab</div>
<div class="MsoNormal">
Enable Rapid fail protection should be checked and change Failures to 1</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
What this will help you is to catch application pool crash easliy. The default value for failures is 5.Which means for the first 5 crashes the application pool will recover itself.So if your goal of testing is to detect crash on load, you might need 5 times load to replicate the issue.</div>
Anonymoushttp://www.blogger.com/profile/15852909723625631148noreply@blogger.com4tag:blogger.com,1999:blog-5183759279662107312.post-68669556176835603362012-04-24T21:01:00.000-07:002012-04-24T21:01:11.281-07:00ColdFusion WebSocket Part5 : Publish WorkflowChannel Listener CFC is the Control Panel for your ColdFusion WebSocket application.<br />
All the data that is published in your channel passes through your Channel Listener CFC. In this blog post I will explain the various Channel Listener functions that are called when a data is published.<br />
<br />
A Channel Listener CFC is associated with a channel. All the sub-channels of that channel also will be controlled by the same Channel Listener CFC.To see sample code on how to associate a channel with a Channel Listener CFC check my <a href="http://www.eveysijo.com/2012/03/coldfusion-websocket-part4restricting.html" target="_blank">previous blog entry</a><br />
Consider a scenarios where you have four clients C1 , C2, C3 and C4.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifw8Sgri6repA6H_6W4AK4bGDzfG5T8wgEbqwveUplu2eLyt4x_1UwE7oqxahbSnGNYThUKwM1wqzHQKuROOjPg_yB04stcDfpfy13rdE9swfc4YZ2KhF2yu9DQAVflE_HIlvCy3TEDjw/s1600/channels.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifw8Sgri6repA6H_6W4AK4bGDzfG5T8wgEbqwveUplu2eLyt4x_1UwE7oqxahbSnGNYThUKwM1wqzHQKuROOjPg_yB04stcDfpfy13rdE9swfc4YZ2KhF2yu9DQAVflE_HIlvCy3TEDjw/s400/channels.png" width="400" /></a></div>
<blockquote class="tr_bq">
Consider a case where <i>News</i> is the main channel. <i>Sports </i>and <i>Politics </i>are sub-channels of News. <i>Tennis </i>is a sub-channel of sports.</blockquote>
C1 is subscribed to <i>News</i><br />
C2 is subscribed to <i>News.Sports</i><br />
C3 is subscribed to <i>News.Sports.Tennis</i><br />
C4 is subscribed to <i>News.Politics</i>.<br />
<br />
<br />
<br />
Whenever data has to be published over the channel, four Channel Listener functions are called internally.<br />
<br />
<ol>
<li><i><b>AllowPublish</b></i> - Verifies if the publisher has the right to publish. If this function returns true, the message is eligible to be published.</li>
<li><i><b>BeforePublish</b></i>- This method can be used to format the message.</li>
<li><i><b>CanSendMessage</b></i>- This method is executed for each client. This function validates if each of the clients is eligible to receive the message.</li>
<li><b><i>BeforeSendMessage</i></b>-This method is executed for each client. This method can be used to format the message differently for each client.</li>
</ol>
These functions are called in a predefined order and a developer or user has no control over the order of function call. Developers can customize the logic inside these channel Listener functions to implement any business logic.
<br />
<br />
See the following diagram which shows the flow of data as well as the order of function calls during a data publish.<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqudeJIuQFaPTQseKcjgSMJxXojxyg9yHpUzFk6JHV-dTROSO_REYg7Y0O7u5GJtWTRA8fnepstE3uIz3XcQwIt0zrxS5tUCXVS3DAccgdO1uBoFWJip_sOy4i9OunuDY5apqZDniW7Q0/s1600/channellistener3.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="498" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqudeJIuQFaPTQseKcjgSMJxXojxyg9yHpUzFk6JHV-dTROSO_REYg7Y0O7u5GJtWTRA8fnepstE3uIz3XcQwIt0zrxS5tUCXVS3DAccgdO1uBoFWJip_sOy4i9OunuDY5apqZDniW7Q0/s640/channellistener3.png" width="640" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
The above diagram also explains how different clients receive data.<br />
For example C2 - subscribed to News sports will receive any data that comes on News.Sports and its sub-channels(In this case News.Sports.Tennis).<br />
C2 will not receive data on any sibling channel(News.Politics) or parent channel(News).<br />
Also you can see that C1 subscribed to News will receive data on all channels.<br />
<br />
For more reference and code samples on ColdFusion WebSocket see the previous entries in this ColdFusion WebSocket blog series<br />
<br />
<ul>
<li><a href="http://www.eveysijo.com/2012/02/coldfusion-websocket-for-absolute.html" target="_blank">ColdFusion WebSocket Part1:Getting Started</a></li>
<li><a href="http://www.eveysijo.com/2012/02/coldfusion-websocket-part2how-do-i.html" target="_blank">ColdFusion WebSocket Part2:How do I publish Messages?</a></li>
<li><a href="http://www.eveysijo.com/2012/02/coldfusion-websocket-part3-interpreting.html" target="_blank">ColdFusion WebSocket Part3: Interpreting response</a></li>
<li><a href="http://www.eveysijo.com/2012/03/coldfusion-websocket-part4restricting.html" target="_blank">ColdFusion WebSocket Part4:Restricting Right to Publish</a></li>
</ul>
<br />
<div>
<br /></div>Anonymoushttp://www.blogger.com/profile/15852909723625631148noreply@blogger.com4tag:blogger.com,1999:blog-5183759279662107312.post-17983825914352035922012-03-11T23:33:00.000-07:002012-03-25T10:32:52.470-07:00ColdFusion WebSocket Part4:Restricting Right to Publish<br />
The very first and easy step to setup WebSocket is to use the default Channel Listener as I described in my first blog entry. But if you need to implement business logic to control the WebSocket system in your application you will have to use a custom Channel Listener CFC. Channel Listener has six methods which can be used to control who will publish/receive data as well as how to present data to different users.<br />
In this blog I am explaining about how to control who all can publish data.<br />
Consider the case of a forum. You have three types of users-Moderators, Members and Guests. Mostly you don't want guests to publish messages.<br />
Let’s try to implement this restriction in your WebSocket Application.<br />
You need a custom Channel Listener CFC. So let’s specify that in your Application.cfc<br />
<div>
<br /></div>
<br />
<br />
<pre style="background-color: #fff7e6; border: 1px solid#999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>component
{
this.name="forumapp";
this.wschannels=[{name="forum",cfclistener="forumListener"}];
}
</code></pre>
<br />
In a real-life application you might associate the type of user based on the credentials given during authentication. Here in my index.cfm, I am specifying the membership type of the user as a custom option while subscribing to the channel(See <span style="color: blue;">highlighted </span>code).<br />
<br />
<pre style="background-color: #fff7e6; border-bottom-color: rgb(153, 153, 153); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: solid; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"><code><script type="text/javascript">
function mymessagehandler( msgobj)
{
var message = ColdFusion.JSON.encode(msgobj);
if(msgobj.data!= null)
message=msgobj.data;
else
switch(msgobj.reqType)
{
case "welcome":
message="Socket Connection established";
break;
case "subscribe" :
message ="Subscription successful";
document.getElementById("subscribeme").disabled=true;
break;
case "publish" :
message ="Message Published";
break;
}
var txt=document.getElementById("myDiv");
txt.innerHTML +=message +"<br>";
}
function publishmessage()
{
var msg = document.getElementById("message").value;
mycfwebsocketobject.publish("forum",msg );
}
function subscribe()
{
var usertype= document.getElementById("usertype").value;
// Type of user is Guest/Moderator/Member as user chooses in the form
<b><span style="color: blue;">mycfwebsocketobject.subscribe("forum",{utype:usertype});</span></b>
}
function myerrhndler(errobj)
{
var message=ColdFusion.JSON.encode(errobj);
switch(errobj.reqType)
{
case "welcome":
message="Socket Connection not established";
break;
case "subscribe" :
message ="Subscription Failed";
break;
case "publish" :
message ="Publish Failed";
break;
}
var txt=document.getElementById("myDiv");
txt.innerHTML +=message +"<br>";
}
</script>
<cfwebsocket name="mycfwebsocketobject"onmessage="mymessagehandler" onerror="myerrhndler">
<cfform >
Status: <select id= "usertype">
<option value="Guest">Guest</option>
<option value="Moderator">Moderator</option>
<option value="Member">Member</option>
</select>
<input id="subscribeme" type="button" value="Connect ME to Forum" onclick="subscribe();"><br>
Message: <input id ="message" type="text" >
<input type="button" onclick="publishmessage();" value="Publish Message">
</cfform>
<cfdiv id="myDiv"></cfdiv>
</code></pre>
<br />
We have specified <i>forumListener </i>as the name of ChannelListnter for the channel <i>forum </i>in our Application.cfc Let’s try to implement the logic to restrict the right to publish in the forumListener.CFC. Here we are overriding two methods- <i>allowSubscribe</i>,<i>allowPublish </i>of the default Channel Listener to implement this restriction<br />
<pre style="background-color: #fff7e6; border-bottom-color: rgb(153, 153, 153); border-bottom-style: solid; border-bottom-width: 1px; border-image: initial; border-left-color: rgb(153, 153, 153); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(153, 153, 153); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(153, 153, 153); border-top-style: solid; border-top-width: 1px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; width: 100%;"><code>component extends="CFIDE.websocket.ChannelListener"
{
public boolean function allowSubscribe(Struct subscriberInfo)
{
if(structKeyExists(subscriberInfo,"utype"))
{
<span style="color: #666666;">//Validating if a user has publish right & adding a new key to ConnectionInfo</span>
if(subscriberInfo.utype eq "Moderator" or subscriberInfo.utype eq "Member" )
subscriberInfo.connectioninfo.havepublishright="true";
return true;
}
return false;
}
public boolean function allowPublish(Struct publisherInfo)
{
if(structKeyExists(publisherInfo.connectioninfo,"havepublishright"))
if(publisherInfo.connectioninfo.havepublishright)
return true;
return false;
}
}
</code></pre>
<br />
You can download this complete example from <a href="http://dl.dropbox.com/u/33017881/blog/forumexample.zip" target="_blank">here</a><br />
<br />
We have implemented these functions keeping in my mind many application scenarios. If you are stuck at some point as to how to implement particular business logic, it would be worth a look at the ChannelListener functions. In the next blog I will try to explain more on the usage of different Channel Listener functions<br />
<br />
For more reference and code samples on ColdFusion WebSocket see the previous entries in this ColdFusion WebSocket blog series<br />
<br />
<ul>
<li><a href="http://www.eveysijo.com/2012/02/coldfusion-websocket-for-absolute.html" target="_blank">ColdFusion WebSocket Part1:Getting Started</a></li>
<li><a href="http://www.eveysijo.com/2012/02/coldfusion-websocket-part2how-do-i.html" target="_blank">ColdFusion WebSocket Part2:How do I publish Messages?</a></li>
<li><a href="http://www.eveysijo.com/2012/02/coldfusion-websocket-part3-interpreting.html" target="_blank">ColdFusion WebSocket Part3: Interpreting response</a></li>
</ul>
<br />
<br />Anonymoushttp://www.blogger.com/profile/15852909723625631148noreply@blogger.com4tag:blogger.com,1999:blog-5183759279662107312.post-76377788701833267772012-02-28T04:38:00.002-08:002012-02-28T04:38:33.197-08:00ColdFusion WebSocket Part3: Interpreting responseIn the previous blog, we discussed how to publish messages. Here, let me explain how to interpret ColdFusion WebSocket responses.<br />
<br />
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.<br />
Look at the sample snippet:<br />
<blockquote style="background-color: #fff7e6; border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-image: initial; border-left-style: solid; border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px;">
<code>
var cnt= mywsobj.getSubscriberCount(channelname);<br />
</code>
</blockquote>
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.<br />
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.<br />
Let us now try to crack the responses <span style="font-family: "Times New Roman","serif"; font-size: 12.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-US;">received</span>.<br />
<div>
In the examples given in my <a href="http://eveysijo.blogspot.in/2012/02/coldfusion-websocket-part2how-do-i.html" target="_blank">previous blog post</a>, 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.</div>
<div>
Firstly, let us divide the messages that come to your WebSocket object in two ways depending on the value of code key.<br />
<ul>
<li>If code key is a 0 , it's a successful message.</li>
<li>If the code key is -1 or 4001, it's a failure message.</li>
</ul>
</div>
<div>
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.<br />
<br />
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.<br />
<blockquote style="background-color: #fff7e6; border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-image: initial; border-left-style: solid; border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px;">
<code><span style="font-family: 'Times New Roman';"><br /></span>
<span style="font-family: monospace;">if(messageobj.type == "data")</span><br />
<span style="font-family: monospace;"> message=messageobj.data;</span><br />
<span style="font-family: monospace;">else</span><br />
<span style="font-family: monospace;">if (messageobj.type == "response") {</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>switch(messageobj.reqType)</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>case 'welcome' : </span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>message="Websocket connection Established";</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span> break;</span><br />
<span style="font-family: monospace;"><br /></span><span class="Apple-tab-span"> </span><span class="Apple-tab-span" style="white-space: pre;"> </span>case "subscribe" : <span style="font-family: monospace;"><br /></span>
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>message="Subscription successfull";</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span> break;</span><br />
<span style="font-family: monospace;"><br /></span> case "getSubscriberCount" : <span style="font-family: monospace;"><br /></span>
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>message="No:of subscribers= " + messageobj.subscriberCount;</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span> break;</span><br />
<span style="font-family: monospace;"><br /></span> case "getSubscriptions" : <span style="font-family: monospace;"><br /></span>
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span> if (messageobj.channels.length >= 1) {</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>message=" You have subscribed to "</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>for (var j = 0; j < messageobj.channels.length; j++)</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>if(j>0) message +=", ";</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>message += messageobj.channels[j].id;</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}else message ="You have not subscribed to any channels"</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span> break;</span><br />
<span style="font-family: monospace;"><br /></span> case "publish":<span style="font-family: monospace;"><br /></span>
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span> message="Message successfully published";</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>break;</span><br />
<span style="font-family: monospace;"><br /></span> case "invokeAndPublish":<span style="font-family: monospace;"><br /></span>
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span> message="Method invoked and message successfully published";</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>break;</span><br />
<span style="font-family: monospace;"><br /></span> case "invoke":<span style="font-family: monospace;"><br /></span>
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span> message="Method invoked <br />" ;</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>message=messageobj.data +"<br />";</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>break;</span><br />
<span style="font-family: monospace;"><br /></span> <span class="Apple-tab-span" style="white-space: pre;"> </span>default: message = ColdFusion.JSON.encode(messageobj);<span style="font-family: monospace;"><br /></span>
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>break;</span><br />
<span style="font-family: monospace;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span><br />
<span style="font-family: monospace;">}</span><br />
</code>
</blockquote>
<br />
In the code, I have just customized the message that needs to be displayed for each kind of response.<br />
Instead, you can call different JavaScript functions to update your UI controls with appropriate data.<br />
Also, you can use similar code in error handler to handle error situations.<br />
<a href="https://sites.google.com/site/cfblogsite/responsehandling.zip?attredirects=0&d=1">Download this basic code sample</a> which you can use to perform almost all WebSocket operations. It also displays customized message for each response.<br />
<br />
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.<br />
<br />
<br />
<br />
<br /></div>Anonymoushttp://www.blogger.com/profile/15852909723625631148noreply@blogger.com6tag:blogger.com,1999:blog-5183759279662107312.post-52948896177782581312012-02-22T06:53:00.000-08:002012-02-28T20:53:15.404-08:00ColdFusion WebSocket Part2:How do I publish Messages?One major advantage of using ColdFusion WebSocket is that it enables you to send data to multiple clients at one go.That is, the server can publish message to multiple clients and each client can publish message to multiple clients.<br />
So lets get familiar with the different ways to publish data.<br />
<br />
<ul>
<li><b>Publish from server</b>: If you want to do a publish from ColdFusion server you need a CFML function, and that function is <b>wsPublish</b>.</li>
<li><b>Publish from Client:</b>When a client wants to publish a data, you need a JavaScript function.This can either be <b>publish </b>or <b>invokeAndPublish </b>methods associated with a ColdFusion WebSocket object.</li>
</ul>
<br />
<br />
<b>Using ColdFusion Function:</b><br />
Here is a sample code to publish a simple message from Server to the channel -publishdemochannel.<br />
<blockquote style="background-color: #fff7e6; border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-image: initial; border-left-style: solid; border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px;">
<code>
Wspublish("publishdemochannel","Welcome to publishdemochannel");</code></blockquote>
Instead of the simple message you can also publish different types of data, like array, struct and query using wspublish.<br />
<br />
<b>Using JavaScript Methods:</b><br />
ColdFusion WebSocket object has got two methods- publish and invokeAndPublish which can be used to publish message from client side. If you are using a simple JavaScript publish, you should have the message that needs to be published. You can use invokeAndPublish, if you have a function defined in your cfc which will be able to provide the message that needs to be published.<br />
<br />
<b>JavaScript </b><b>publish:</b><br />
Once you have created your JavaScript websocket object using cfwebsocket tag you can invoke Javascript publish method to publish a messages over channel/subchannel.<br />
This can be done in your <b>JavaScript code block </b>For example,<br />
<blockquote style="background-color: #fff7e6; border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-image: initial; border-left-style: solid; border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px;">
<code>mycfwebsocketobject.publish("publishdemochannel","My test message to publishdemochannel" );<br />
</code></blockquote>
<br />
Here we are publishing a simple String. You can also pass a string variable,a javascript array or even javascript object as argument to the publish method.<br />
For example you could publish an array to a channel like below inside a JavaScript code block.<br />
<blockquote style="background-color: #fff7e6; border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-image: initial; border-left-style: solid; border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px;">
<code>
<br />
mycfwebsocketobject.publish("publishdemochannel",["Jan","Feb"] );<br />
<br />
</code></blockquote>
or you can use a Javascript array variable like below<br />
<blockquote style="background-color: #fff7e6; border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-image: initial; border-left-style: solid; border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px;">
<code>var myarr= new Array();</code><br />
<code>myarr[1]="Jan";</code><br />
<code>myarr[2]="Feb";</code><br />
<code>myarr[3]="Mar";</code><br />
<code>mycfwebsocketobject.publish("publishdemochannel",myarr);</code><br />
<code><br /></code></blockquote>
<code>
</code>
<br />
You can also publish a JavaScript object using the publish method.<br />
<blockquote style="background-color: #fff7e6; border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-image: initial; border-left-style: solid; border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px;">
<code>var myobj= new Object();<br />
myobj.fname="Evelin";<br />
myobj.lname="Varghese";<br />
myobj.age=28;<br />
myobj.gender="Female";<br />
mycfwebsocketobject.publish("publishdemochannel",myobj);<br />
</code>
</blockquote>
<br />
<br />
<b>Using invokeAndPublish:</b><br />
As I mentioned earlier invokeAndPublish is used to call a CFC function.The value returned by the function is published over the channel.<br />
For example you have the employee id of the person, but you don't have the name of the person, you can use invokeAndPublish method of websocket to invoke the cfc method that will take the empid and will return the message that needs to be published.<br />
<blockquote style="background-color: #fff7e6; border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-image: initial; border-left-style: solid; border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px;">
<code>mycfwebsocketobject.invokeAndPublish("publishdemochannel", "employee", "processMessage",[389,"Hello "]);</code></blockquote>
The value that the fucntion returns will be published to the channel specified:<br />
<blockquote style="background-color: #fff7e6; border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-image: initial; border-left-style: solid; border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px;">
<code>component<br />
{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public function processMessage(eid,msg)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
//Write your logic to get the empname based on empid<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> var empname="Evelin";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> return msg & " " & empname;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}<br />
</code></blockquote>
<div>
<br /></div>
<a href="https://sites.google.com/site/cfblogsite/publishexample.zip?attredirects=0&d=1">Download this code sample</a> which publishes different types of messages.<br />
<br />
With the description here I hope you will be able to get started with your application to manage real time data. In the next few blogs I will try to explain more scenarios that you will come across in your ColdFusion WebSocket application development.<br />
For more on How to handle responses from server checkout <a href="http://eveysijo.blogspot.in/2012/02/coldfusion-websocket-part3-interpreting.html" target="_blank">my next blog entry </a><br />
<br />Anonymoushttp://www.blogger.com/profile/15852909723625631148noreply@blogger.com3tag:blogger.com,1999:blog-5183759279662107312.post-92091783916275381102012-02-18T05:56:00.000-08:002012-02-23T03:36:46.464-08:00ColdFusion WebSocket Part1:Getting Started<b>ColdFusion Websockets?What can I do with it?</b><br />
<div>
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.<br />
<br /></div>
<div>
<b>But how is it different?</b></div>
<div>
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.<br />
<br /></div>
<div>
<b>So how do I implement it?</b></div>
<div>
You need to have ColdFusion 10 to make use of WebSocket. If you have not installed it yet, get it from <a href="http://labs.adobe.com/technologies/coldfusion10/">here</a> .<br />
Here is a very simple websocket channel implementation with minimal lines of code<br />
<br />
<b>Step1: </b> Specify the channelname in Application.cfc file</div>
<div>
This is absolutely mandatory if you need to establish websocket channel communication.<br />
So your simplest Application.cfc will have the below code.<br />
<blockquote style="background-color: #fff7e6; border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-image: initial; border-left-style: solid; border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px;">
<code>
component<br />
{<br /> this.name="wsdemoapp";<br /> this.wschannels=[{name="stocks"}];<br />
}<br />
</code>
</blockquote>
</div>
<div>
Note that the above code is in ColdFusion Script syntax. We have specfied the name of the websocket channel as stocks</div>
<div>
<br />
<b>Step2: </b>Use the new cfwebsocket tag to establish connection from your cfm page to your channel<br />
<br />
<blockquote style="background-color: #fff7e6; border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-image: initial; border-left-style: solid; border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px;">
<code>
<cfwebsocket name="mycfwebsocketobject" onmessage="mymessagehandler" subscribeto="stocks" ></code></blockquote>
<br />
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.<br />
<br />
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.<br />
<br /></div>
<div>
<b>Step3:</b>Define the message handler<br />
<blockquote style="background-color: #fff7e6; border-bottom-style: solid; border-bottom-width: 1px; border-color: initial; border-image: initial; border-left-style: solid; border-left-width: 1px; border-right-style: solid; border-right-width: 1px; border-top-style: solid; border-top-width: 1px;">
<code><script type="text/JavaScript"><br /> function mymessagehandler(messageobj)<br />
{<br />
//Converting the JS object to a string and display in "myDiv"<br />
var message = ColdFusion.JSON.encode(messageobj);<br /> var txt=document.getElementById("myDiv");<br /> txt.innerHTML +=message +"<br>";<br />
}<br />
<br />
</script><br />
</code></blockquote>
<div>
Don't forget include a div with id "myDiv" in your cfm page to display the messages .<br />
<br />
<b>Step4:</b>Run your cfm page.<br />
If you see a string of below format in your cfm page the WebsSocket Connection is successful<br />
<br />
{"clientid":41244233,"ns":"coldfusion.websocket.channels","reqType":"welcome","code":0,"type":"response","msg":"ok"}
<br />
<br />
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 <a href="https://sites.google.com/site/cfblogsite/sample1.zip?attredirects=0&d=1">here.</a><br />
<br />
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.<br />
Happy playing with ColdFusion 10!!!<br />
<br />
For more on how to publish messages check out <a href="http://eveysijo.blogspot.in/2012/02/coldfusion-websocket-part2how-do-i.html">ColdFusion WebSocket Part2: How do I publish Message?</a> </div>
</div>Anonymoushttp://www.blogger.com/profile/15852909723625631148noreply@blogger.com7tag:blogger.com,1999:blog-5183759279662107312.post-82604223230941429972011-05-27T02:22:00.000-07:002011-05-27T02:22:53.138-07:00Switching between frames and WebDriverI have web page which has two frames -navigator and content.I have to click on a link in the navigator and the content of the frame 'content' changes.Now I have to input some value in the content frame.I use IDE to record and generate code in the junit4 webdriver format.<br />
selenium.selectFrame("navigator");<br />
selenium.click("link=mylink");<br />
selenium.selectFrame("relative=up");<br />
selenium.selectFrame("content");<br />
selenium.type("myinputfield", "7");<br />
<br />
Now when I try to run this code I get error at relative=up.So I google and find that this is not the way to write webdriver code.I rewrote the entire code in the webdriver style coding .So now I have this code.<br />
driver.switchTo().frame("navigator"); <br />
WebElement my_nav_link = driver.findElement(By.partialLinkText("MyLinkText"));<br />
my_nav_link.click();<br />
driver.switchTo().frame("content");<br />
WebElement myinputfieldelem= driver.findElement(By.id("myinputfield"));<br />
myinputfieldelem.sendKeys("7");<br />
Now when I try again , it errors out at frame not found for content .<br />
So I try to debug a bit and I add the below code just before switch to frame to see all the frames present.<br />
List<WebElement> frameset=driver.findElements(By.tagName("frame"));<br />
if(frameset.size()>0)<br />
{<br />
for (WebElement framename :frameset)<br />
{<br />
System.out.println("frame id:" + framename.getAttribute("name"));<br />
}<br />
}<br />
There are no frames ,so that means I am now in navigator frame and I have to go one level up to find the frame content.<br />
So I tried <br />
driver.switchTo().frame("relative=up");<br />
This also failed.Now I am clueless and and could not figure out what to do.After some googling i found out another option which saved me and the final code which works is as below.<br />
driver.switchTo().frame("navigator"); <br />
WebElement my_nav_link = driver.findElement(By.partialLinkText("MyLinkText"));<br />
my_nav_link.click();<br />
driver.switchTo().defaultContent();<br />
driver.switchTo().frame("content");<br />
WebElement myinputfieldelem= driver.findElement(By.id("myinputfield"));<br />
myinputfieldelem.sendKeys("7");<br />
So the outcome is you have to use switchTo().defaultContent() to be able to come out of a frame and go to a sibling frame.Anonymoushttp://www.blogger.com/profile/15852909723625631148noreply@blogger.com27tag:blogger.com,1999:blog-5183759279662107312.post-23595416368638076612011-05-25T23:37:00.000-07:002011-05-26T01:04:12.606-07:00Selenium First steps<div dir="ltr" style="text-align: left;" trbidi="on">I am in the process of evaluating using selenium for automating a web interface.So first of all I don't know anything about it and I am trying o figure out how things work in selenium.<br />
I first installed the selenium ide as a plugin to firefox from <a href="http://seleniumhq.org/">here</a>. <br />
I recorded a sample script and did a play back.Yes my first test worked like a charm .<br />
Now I see there code and it is in html.I found out that there is way to generate code in Junit4 format (Options->Format->Junit 4).I changed the format to Junit 4.Now the play back option is gone.<br />
So how do I play back Junit4 test cases of selenium??<br />
Then I see there is a selenium server.Now the whole confusion starts and I know these are starting roadblocks.<br />
There is selenium2 as well as selenium 1 and there is something called webdriver also.So I thought I will install the latest and I installed the selenium 2 server .I started it from console and I thought I will be able to run junit tests from there.But once I started server from console I realized ,there is no option to start scripts from there. Now I find something called as java driver and there is some firefox driver. I was confused as to what to use where.Later I found that java driver is used along with eclipse.So followed the <a href="http://seleniumhq.org/docs/appendix_installing_java_driver_client.html">steps</a> to use the java driver in eclipse.OK, now I record the scripts in selenium ide in junit format and copy paste that code to eclipse and from here I can run the junit test cases.<br />
<br />
So I am happy with the setup.But When I look at the test cases which has SeleneseTestCase is reported as deprecated by Eclipse.So What should I Do?Now I google again and I find that there is a webdriver format supported IDE plugin for firefox which get from <a href="https://addons.mozilla.org/en-us/firefox/addon/webdriver-backed-formatters/">here</a>. After installing the plugin I could generate code in Junit4 webdriver format and there is no warnings.<br />
<br />
</div>Anonymoushttp://www.blogger.com/profile/15852909723625631148noreply@blogger.com1