Using ColdFusion WebSocket

What is WebSocket

WebSocket is a protocol for two-way communication with a remote host over TCP protocol exposed through JavaScript interface in HTML 5 compliant browsers. WebSocket facilitates communication between hosts in both the directions simultaneously.

How is WebSocket different from conventional communication models

WebSocket is based on HTML 5 server Push Technology. The communication request is initiated by the publisher or central server following a publish/subscribe model.

This is unlike the request/response model that HTTP relies on, which has drawbacks such as request time-out and browser refresh.

In the case of WebSocket, a client subscribes to various information channels. If new content is available in the channel, it is pushed to the clients. This method has the following advantages:

  • Simultaneous bi-directional communication

  • Lesser browser reloads

  • Ease in creating applications that handle real-time data

ColdFusion and WebSocket

ColdFusion 10 implements WebSocket by providing a messaging layer for the WebSocket protocol, which you can control using CFML and JavaScript.

The messaging layer has features that make WebSocket reliable and scalable. Using WebSocket, you can develop real-time applications for stock, charting, online gaming, social networking, dashboard for various purposes, and monitoring.

Browsers and fallback

ColdFusion leverages the browser-supported HTML5 native WebSocket to provide server controlled client-to-client communication. If a browser currently does not have HTML5 support or the native WebSocket support, ColdFusion uses Flash player as the fallback option. Fallback to Flash is smooth and automatic.

See also, Browser support for WebSocket.

The following table provides the fallback plan for ColdFusion WebSocket:


What happens

Native WebSocket support available

Establishes a WebSocket connection.

Native WebSocket unavailable, but Flash is installed

Automatically falls back to Flash.

No native WebSocket support or Flash installation

Sends a message indicating that the connection is not successful.

To proceed, either move on to a compliant browser or install Flash.

WebSocket communication models

ColdFusion WebSocket offers the following two communication models:
  • Broadcast: Follows a subscriber/publisher model where hosts include multiple clients and server. Publishing is possible in either of the following ways:

    • From client-side (by way of the server)

    • Initiation from the server without client involvement

  • Point-to-point: A bi-directional communication between a specific client and server. This model does not involve any other hosts/multiple clients.