Wednesday, February 22, 2012

ColdFusion 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.
So lets get familiar with the different ways to publish data.

  • Publish from server: If you want to do a publish from ColdFusion server you need a CFML function, and that function is wsPublish.
  • Publish from Client:When a client wants to publish a data, you need a JavaScript function.This can either be publish or invokeAndPublish methods associated with a ColdFusion WebSocket object.

Using ColdFusion Function:
Here is a sample code to publish a simple message from Server to the channel -publishdemochannel.
Wspublish("publishdemochannel","Welcome to publishdemochannel");
Instead of the simple message you can also publish different types of data, like array, struct and query using wspublish.

Using JavaScript Methods:
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.

JavaScript  publish:
Once you have created your JavaScript websocket object using cfwebsocket tag you can invoke Javascript publish method to publish a messages over channel/subchannel.
This can be done in your JavaScript code block For example,
mycfwebsocketobject.publish("publishdemochannel","My test message to publishdemochannel" );

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.
For example you could publish an array to a channel like below inside a JavaScript code block.

mycfwebsocketobject.publish("publishdemochannel",["Jan","Feb"] );

or you can use a Javascript array variable like below
var myarr= new Array();

You can also publish a JavaScript object using the publish method.
var myobj= new Object();

Using invokeAndPublish:
As I mentioned earlier invokeAndPublish is used to call a CFC function.The value returned by the function is published over the channel.
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.
mycfwebsocketobject.invokeAndPublish("publishdemochannel", "employee",      "processMessage",[389,"Hello "]);
The value that the fucntion returns will be published to the channel specified:
public function processMessage(eid,msg)
            //Write your logic to get the empname based on empid
    var empname="Evelin";
    return msg & " " & empname;

Download this code sample which publishes different types of messages.

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.
For more on How to handle responses from server checkout my next blog entry 


  1. Hi,
    I want to publish message to particular client by clientid is it possible?if yes, then How?

    by answer,perhaps you'll make by day.
    Thank you.

  2. Say you wanted to publish from a database table as people updated it? Can this be used and tied to a particular table? It would be neat to have people enter comments and see them shown to all live.