Description

Lets you create the WebSocket object in your CFM template. The tag creates a reference to the WebSocket JavaScript object at the client-side

Category

History

ColdFusion 11: Added a new attribute, secure.

ColdFusion 10: Added this tag.

Syntax

name="websocketName" 
onMessage="JavaScript function name" 
onOpen="JavaScript function name" 
onClose="JavaScript function name" 
onError="JavaScript function name" 
useCfAuth=true|false 
subscribeTo="channel_list"
secure="true|false">

Attribute

Attribute

Req/Opt

Descriptions

name

Required

The name of the WebSocket object. This is the reference to the JavaScript objects that are used to call WebSocket JavaScript functions.

onMessage

Required

The JavaScript function that is called when the WebSocket receives a message from the server. It supports the argument, aEvent. This argument is the websocket event that is dispatched from the server.

onOpen

Optional

The JavaScript function that is called when the WebSocket establishes a connection.

onClose

Optional

The JavaScript function that is called when the WebSocket closes a connection.

onError

Optional

The JavaScript function that is called if there is an error while performing an action over the WebSocket connection.

usecfAuth

Optional

If set to true (default), users need not authenticate for WebSocket connection (provided they have already logged in to the application). This is the default value.
If false, users have to specify the credentials for the WebSocket connection.

subscribeTo

Optional

Comma-separated list of channels to subscribe to. You can specify any or all channels set in the Application.cfc.

secure Optional

If true, the web socket communication will happen over SSL.

Example

In the following example,

  • The user is automatically subscribed to the stocks channel.
  • You have specified the channel name in the Application.cfc.
  • The example uses the default channel listener.
  • In the Index.cfm, you specify the channels to which the user can automatically subscribe to (using the attribute subscribeTo) and also define the message handler.
    Application.cfc

{
this.name="websocketsampleapp1";
this.wschannels=[{name="stocks"}];
}

Index.cfm

<script type="text/javascript"> 
function mymessagehandler(aevent, atoken) 
{ 
var message = ColdFusion.JSON.encode(atoken); 
var txt=document.getElementById("myDiv"); 
txt.innerHTML +=message +"<br>"; 
} 
</script> 
<cfwebsocket name="mycfwebsocketobject" onmessage="mymessagehandler" subscribeto="stocks" > 
<cfdiv id="myDiv"></cfdiv>

The code creates a JavaScript WebSocket object named mycfwebsocketobject. If the server sends a message, it calls mymessagehandler function.Since you have specified subscribeTo in the cfwebsocket tag, the WebSocket object automatically subscribes you to the channel stocks.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy