cfwebsocket

Includes the required JavaScript files in your CFM template and creates a global JavaScript reference to the WebSocket Object on the client-side.

 <cfwebsocket name="" onMessage="">

 cfwebsocket(name="", onMessage="");

This tag requires Adobe ColdFusion 10 and up. Not supported on Lucee, OpenBD, etc.

Attribute Reference for the cfwebsocket tag

name

Required: Yes

onMessage

Required: Yes
The JavaScript function that is called when the WebSocket receives a message from the server.

onOpen

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

onClose

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

onError

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

usecfAuth

Required: No
Default: false
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

Required: No
Comma-separated list of channels to subscribe to. You can specify any or all channels set in your Application this.wschannels settings

secure

Required: No
If true, the web socket communication will happen over SSL. CF 11+

Compatibility

ColdFusion:

Version 10+ SSL is only available on CF 11+

Links more information about cfwebsocket

Examples sample code using the cfwebsocket tag


Script Syntax (CF11+)

cfwebsocket(name="ws", onMessage="handleMessage");

Tag Syntax

This example creates a JavaScript WebSocket object named ws. When a message is pushed, the client runs the handleMessage JavaScript function. This example also uses the subscribeTo attribute so it will automatically subscribe to the channel chat.

<cfwebsocket name="ws" onMessage="handleMessage" subscribeTo="chat" />
<script>
  function handleMessage(message)  {
    // Output message object to console
    console.log(message);
  }
</script> 

Sub-channels

Sub-channels are created dynamically and are referenced by dot notation. When a message is broadcasted to a channel all its sub-channels receive the message as well. This only goes from top to bottom and not the other way around. In this example we create a JavaScript object called ws and subscribe to the room-general subchannel of the main chat channel.

<cfwebsocket name="ws" onMessage="handleMessage" subscribeTo="chat.room-general" />

Fork me on GitHub